英文:
How to display the contents of an array { data: { results: [ [Object] ], total: 1, page: 1 }, error: null } in the response?
问题
Here's the translated code part without the request for you:
我有以下的代码片段:
```javascript
let result = null;
await fetch(request)
.then((response) => {
if (response.status === 201) {
console.log("Success");
result = response.json();
} else {
throw new Error("API 服务器出现问题!");
}
})
.catch((error) => {
console.error(error);
});
console.log("响应:", await result);
});
以及我收到的响应是:
[0-0] 成功
[0-0] 响应:{ 数据: { 结果: [ [Object] ], 总数: 1, 页: 1 }, 错误: null }
但在 Postman 中,响应看起来是这样的:
```json
{
"data": {
"results": [
{
"customerId": 123,
"firstName": "Name",
"lastName": "LastName",
"displayName": "Name LastName",
"phoneNumber": "4159436367",
"email": "email@email.com",
"notes": null,
"memberNumber": null,
"highlights": null
}
],
"total": 1,
"page": 1
},
"error": null
}
如何显示响应中数组的内容,以便像在 Postman 中一样查看?
谢谢
我尝试添加了这段代码:
const parsedResponce = result;
const obj = JSON.parse(parsedResponce);
console.log(obj.phoneNumber);
在最后一个
console.log("响应:", await result);
之后,并收到以下错误:
SyntaxError: 位置1处的JSON中意外的令牌“o”
<details>
<summary>英文:</summary>
I have the following piece of code:
let result = null;
await fetch(request)
.then((response) => {
if (response.status === 201) {
console.log("Success");
result = response.json();
} else {
throw new Error("Something went wrong on API server!");
}
})
.catch((error) => {
console.error(error);
});
console.log("Response:", await result);
});
});
and in response I received:
[0-0] Success
[0-0] Response: { data: { results: [ [Object] ], total: 1, page: 1 }, error: null }
But in Postman the response looks like this:
{
"data": {
"results": [
{
"customerId": 123,
"firstName": "Name",
"lastName": "LastName",
"displayName": "Name LastName",
"phoneNumber": "4159436367",
"email": "email@email.com",
"notes": null,
"memberNumber": null,
"highlights": null
}
],
"total": 1,
"page": 1
},
"error": null
}
How to display the contents of an array in response, for viewing like in Postman?
Thanks
I tried to add this code:
const parsedResponce = result;
const obj = JSON.parse(parsedResponce);
console.log(obj.phoneNumber);
After last
console.log("Response:", await result);
And received
SyntaxError: Unexpected token o in JSON at position 1
</details>
# 答案1
**得分**: 0
你可以使用 `JSON.stringify` 来显示格式化的对象。
```javascript
let result = {
"data": {
"results": [
{
"customerId": 123,
"firstName": "Name",
"lastName": "LastName",
"displayName": "Name LastName",
"phoneNumber": "4159436367",
"email": "email@email.com",
"notes": null,
"memberNumber": null,
"highlights": null
}
],
"total": 1,
"page": 1
},
"error": null
};
console.log(JSON.stringify(result, null, 4));
此代码段是一个示例,用于演示如何使用 JSON.stringify
格式化对象。
英文:
You can use JSON.stringify
to show a formatted object.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let result = {
"data": {
"results": [
{
"customerId": 123,
"firstName": "Name",
"lastName": "LastName",
"displayName": "Name LastName",
"phoneNumber": "4159436367",
"email": "email@email.com",
"notes": null,
"memberNumber": null,
"highlights": null
}
],
"total": 1,
"page": 1
},
"error": null
};
console.log(JSON.stringify(result, null, 4));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论