简体   繁体   English

如何访问此 JSON 中的“订单”(名称中带有分号)?

[英]How do I access "order" in this JSON (with semicolons in the name)?

Vijay Anand asked this question yesterday, but it was closed before he got an answer: Vijay Anand昨天问了这个问题,但还没等他回答就被关闭了:

HTTP Response: 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 code: Javascript代码:

var json = response;
var order = json.object.entry.content['m:properties']['d:Order']; // I intend to read Order no from the below response.

Error (example, jsbin.com):错误(例如,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"

Per JSLint , the response is valid JSON.根据JSLint ,响应是有效的 JSON。

json.object.entry is obviously wrong ... but json.object.entry显然是错误的......但是

Q: What is the correct Javascript syntax to access the "order" value (named d:Order ), when m:properties and d:Order both have semicolons in the name?问:当m:propertiesd:Order的名称中都包含分号时,访问“order”值(名为d:Order )的正确 Javascript 语法是什么?

PS: I nominated Vijay's original question for re-opening ... but I'm not optimistic. PS:我提名了Vijay的原题重新开题……但我并不乐观。 Hence my new question.因此我的新问题。

You need to parse the JSON.您需要解析 JSON。 And there's no object property anywhere, it's jut json.entry.content .而且任何地方都没有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.

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