从Json中通过键获取值

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

Get Value from Json by Key

问题

以下是翻译后的JSON响应的关键部分:

{
    "SNGS": {
        "$": {
            "xmlns": "csng",
            "xmlns:ns2": "http://www.w3.org/1999/xlink"
        },
        "Defect": [{
            "$": {
                "id": "DCV19294",
                "ns2:href": "https://mywebsite.com/api/beta//bug/DCSV19294"
            },
            "Field": [{
                "_": "4",
                "$": {
                    "name": "Severity"
                }
            }, {
                "_": "N",
                "$": {
                    "name": "Status"
                }
            }]
        }]
    }
}

你尝试从上述JSON中获取以下键的值:
1] id
2] status

以下是你尝试解析JSON并按键获取值的代码,以及可能导致undefined警报的原因:

const data = '{"SNGS":{"$":{"xmlns":"csng","xmlns:ns2":"http://www.w3.org/1999/xlink"},"Defect":[{"$":{"id":"DCV19294","ns2:href":"https://mywebsite.com/api/beta//bug/DCSV19294"},"Field":[{"_":"4","$":{"name":"Severity"}},{"_":"N","$":{"name":"Status"}}]}}}';
const parsedData = JSON.parse(data);

console.log(parsedData.id); // 这将返回undefined,因为id是在Defect对象内部的
console.log(parsedData.Status); // 这也将返回undefined,因为Status是在Defect对象内部的

要获取id和status的值,你需要按照JSON结构访问它们,如下所示:

const id = parsedData.SNGS.Defect[0].$.id; // 获取id的值
const status = parsedData.SNGS.Defect[0].Field[1].$.name; // 获取status的值

console.log(id); // 输出: DCV19294
console.log(status); // 输出: N

希望这可以帮助你获取所需的值。

英文:

I am having a json response as shown below

{
    "SNGS": {
        "$": {
            "xmlns": "csng",
            "xmlns:ns2": "http://www.w3.org/1999/xlink"
        },
        "Defect": [{
            "$": {
                "id": "DCV19294",
                "ns2:href": "https://mywebsite.com/api/beta//bug/DCSV19294"
            },
            "Field": [{
                "_": "4",
                "$": {
                    "name": "Severity"
                }
            }, {
                "_": "N",
                "$": {
                    "name": "Status"
                }
            }]
        }]
    }
}

I am trying to get value of below keys:
1] id
2] status

> I tried to parse json & get value by key using below code but I am getting undefined in alert. WHY?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data=&#39;{&quot;SNGS&quot;:{&quot;$&quot;:{&quot;xmlns&quot;:&quot;csng&quot;,&quot;xmlns:ns2&quot;:&quot;http://www.w3.org/1999/xlink&quot;},&quot;Defect&quot;:[{&quot;$&quot;:    {&quot;id&quot;:&quot;DCV19294&quot;,&quot;ns2:href&quot;:&quot;https://mywebsite.com/api/beta//bug/DCSV19294&quot;},&quot;Field&quot;:[{&quot;_&quot;:&quot;4&quot;,&quot;$&quot;:{&quot;name&quot;:&quot;Severity&quot;}},{&quot;_&quot;:&quot;N&quot;,&quot;$&quot;:{&quot;name&quot;:&quot;Status&quot;}}]}]}}&#39;

const parsedData = JSON.parse(data);

console.log(parsedData.id);
console.log(parsedData.Status);

<!-- end snippet -->

I want to get id i.e.DCV19294 & status i.e. N from above Json. please help.

答案1

得分: 0

我们不知道数据提取的逻辑。您可以像下面这样直接访问它。但是,如果您能多加解释,可能会有更好的逻辑。

const data = '{"SNGS":{"$":{"xmlns":"csng","xmlns:ns2":"http://www.w3.org/1999/xlink"},"Defect":[{"$":{"id":"DCV19294","ns2:href":"https://mywebsite.com/api/beta//bug/DCSV19294"},"Field":[{"_":"4","$":{"name":"Severity"}},{"_":"N","$":{"name":"Status"}}]}]}}';

const parsedData = JSON.parse(data);

const id = parsedData.SNGS.Defect[0].$.id;
const name = parsedData.SNGS.Defect[0].Field[1]._;

console.log(id, name);

如果有更多解释,可能会有更好的逻辑。

英文:

We don't know the logic behind the extraction of data. You could access it directly like below. However there could be a better logic if you explain it a bit more.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data=&#39;{&quot;SNGS&quot;:{&quot;$&quot;:{&quot;xmlns&quot;:&quot;csng&quot;,&quot;xmlns:ns2&quot;:&quot;http://www.w3.org/1999/xlink&quot;},&quot;Defect&quot;:[{&quot;$&quot;:    {&quot;id&quot;:&quot;DCV19294&quot;,&quot;ns2:href&quot;:&quot;https://mywebsite.com/api/beta//bug/DCSV19294&quot;},&quot;Field&quot;:[{&quot;_&quot;:&quot;4&quot;,&quot;$&quot;:{&quot;name&quot;:&quot;Severity&quot;}},{&quot;_&quot;:&quot;N&quot;,&quot;$&quot;:{&quot;name&quot;:&quot;Status&quot;}}]}]}}&#39;

const parsedData = JSON.parse(data);

const id = parsedData.SNGS.Defect[0].$.id
const name = parsedData.SNGS.Defect[0].Field[1]._

console.log(id, name)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 15:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626272.html
匿名

发表评论

匿名网友

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

确定