简体   繁体   English

如何使用 JSON 查询将数组转换为字符串

[英]How to convert array to String using JSON Query

{
   "pull_request": {
     "patch_url": null,
   },
   "created_at": "2012-11-14T15:25:33Z",
   "comments": 0,
   "labels": [
     {
       "color": "ededed
     }
   ],
   "id": 8356941,
   "assignees": [
      {
        "login": "Paul",
        "id": 444
      },
      {
        "login": "Steve",
        "id": 222
       }
    ]

By JSON Query通过 JSON 查询

assignees[*].login , getting below output. assignees[*].login ,低于输出。

["Paul","Steve"]

But required as comma separated string without quotes as below,但需要作为逗号分隔的字符串,不带引号,如下所示,

Paul, Steve

How to do this in JSON Query?如何在 JSON 查询中做到这一点?

You can use JSON.stringify as below:您可以使用 JSON.stringify 如下:

JS JS

 const obj = { "pull_request": { "patch_url": null, }, "created_at": "2012-11-14T15:25:33Z", "comments": 0, "labels": [{ "color": "ededed" }], "id": 8356941, "assignees": [{ "login": "Paul", "id": 444 }, { "login": "Steve", "id": 222 } ] } const myJSON = JSON.stringify(obj); console.log(myJSON);

If you would like to get comma seperated string for the array it can be done using join()如果您想为数组获取逗号分隔的字符串,可以使用join()

 const names = ["Paul","Steve"]; console.log(names.join(", "));

只要您的标题“如何将数组转换为字符串”是正确的,这将起作用:

assignees[*].login.toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM