簡體   English   中英

如何訪問此 JSON 中的“訂單”(名稱中帶有分號)?

[英]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:propertiesd: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM