[英]How do I access "order" in this JSON (with semicolons in the name)?
Vijay Anand昨天问了这个问题,但还没等他回答就被关闭了:
HTTP响应:
{
"entry": {
"@xml:base": "https://API_PROC_SRV/",
"@xmlns": "http://www.w3.org/2005/Atom",
"@xmlns:m": "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
"@xmlns:d": "http://schemas.microsoft.com/ado/2007/08/dataservices",
"id": "https://API_PROC_SRV/A_Order",
"title": {
"@type": "text",
"#text": "A_Order()"
},
"updated": "2020-02-29T07:33:28Z",
"category": {
"@term": "Type",
"@scheme": "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"
},
"link": [],
"content": {
"@type": "application/xml",
"m:properties": {
"d:Order": "123456789"
}
}
}
}
Javascript代码:
var json = response;
var order = json.object.entry.content['m:properties']['d:Order']; // I intend to read Order no from the below response.
错误(例如,jsbin.com):
"TypeError: Cannot read property 'entry' of undefined
at null.js:27:25
at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13924
at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10866"
根据JSLint ,响应是有效的 JSON。
json.object.entry
显然是错误的......但是
问:当m:properties
和d:Order
的名称中都包含分号时,访问“order”值(名为d:Order
)的正确 Javascript 语法是什么?
PS:我提名了Vijay的原题重新开题……但我并不乐观。 因此我的新问题。
您需要解析 JSON。 而且任何地方都没有object
属性,它只是json.entry.content
。
response = `{ "entry": { "@xml:base": "https://API_PROC_SRV/", "@xmlns": "http://www.w3.org/2005/Atom", "@xmlns:m": "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", "@xmlns:d": "http://schemas.microsoft.com/ado/2007/08/dataservices", "id": "https://API_PROC_SRV/A_Order", "title": { "@type": "text", "#text": "A_Order()" }, "updated": "2020-02-29T07:33:28Z", "category": { "@term": "Type", "@scheme": "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" }, "link": [], "content": { "@type": "application/xml", "m:properties": { "d:Order": "123456789" } } } }`; var json = JSON.parse(response); var order = json.entry.content['m:properties']['d:Order']; console.log(order);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.