How to display the contents of an array { data: { results: [ [Object] ], total: 1, page: 1 }, error: null } in the response?

huangapple go评论62阅读模式
英文:

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 = {
    &quot;data&quot;: {
        &quot;results&quot;: [
            {
                &quot;customerId&quot;: 123,
                &quot;firstName&quot;: &quot;Name&quot;,
                &quot;lastName&quot;: &quot;LastName&quot;,
                &quot;displayName&quot;: &quot;Name LastName&quot;,
                &quot;phoneNumber&quot;: &quot;4159436367&quot;,
                &quot;email&quot;: &quot;email@email.com&quot;,
                &quot;notes&quot;: null,
                &quot;memberNumber&quot;: null,
                &quot;highlights&quot;: null
            }
        ],
        &quot;total&quot;: 1,
        &quot;page&quot;: 1
    },
    &quot;error&quot;: null
};
console.log(JSON.stringify(result, null, 4));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月20日 23:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定