英文:
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='{"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);
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='{"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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论