解析属性键中带有特殊字符的JSON对象

时间:2019-06-28 05:05:45

标签: javascript json

我有一个看起来像这样的JSON文件

{
    "samlp:Response": {
        "@attributes": {
            "xmlns:samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
            "ID": "_482d7b9c-3e50-47cb-aa64-4e3655352c64",
            "Version": "2.0",
            "IssueInstant": "2019-06-27T17:02:47.711Z",
            "Destination": "https://jjds-sunrise--cqsupport--c.cs102.visual.force.com/apex/ResponseReceiver",
            "InResponseTo": "Azure_9849028456"
        }
    }
}

尝试使用常规方式(例如,

)解析此JSON时
jsonObject.samlp:Response.@attributes.ID

其中jsonObject是我为此JSON创建的对象。我收到一条错误消息

  

“意外令牌:”

。我做错了吗?还是有其他解析方式?

1 个答案:

答案 0 :(得分:3)

看看Property Accessors。请考虑以下内容:

const jsonObject = {
  "samlp:Response": {
    "@attributes": {
      "xmlns:samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
      "ID": "_482d7b9c-3e50-47cb-aa64-4e3655352c64",
      "Version": "2.0",
      "IssueInstant": "2019-06-27T17:02:47.711Z",
      "Destination": "https://jjds-sunrise--cqsupport--c.cs102.visual.force.com/apex/ResponseReceiver",
      "InResponseTo": "Azure_9849028456"
    }
  }
};


console.log(jsonObject["samlp:Response"]["@attributes"].ID);