javascript - Parsing a JSON object with Special characters in property key - Stack Overflow

I have a JSON file that looks something like this{"samlp:Response": {"@attributes":

I have a JSON file that looks something like this

{
    "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": "",
            "InResponseTo": "Azure_9849028456"
        }
    }
}

While trying to parse this JSON using the normal way i.e.

jsonObject.samlp:[email protected]

where jsonObject is the object I created for this JSON. I am getting an error that says

"unexpected token :"

. Am I doing it wrong or is there other way to parse this?

I have a JSON file that looks something like this

{
    "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./apex/ResponseReceiver",
            "InResponseTo": "Azure_9849028456"
        }
    }
}

While trying to parse this JSON using the normal way i.e.

jsonObject.samlp:[email protected]

where jsonObject is the object I created for this JSON. I am getting an error that says

"unexpected token :"

. Am I doing it wrong or is there other way to parse this?

Share Improve this question edited Jun 28, 2019 at 5:08 Anshu 1,3372 gold badges16 silver badges28 bronze badges asked Jun 28, 2019 at 5:05 Bonish KoiralaBonish Koirala 6518 silver badges16 bronze badges 1
  • 2 You can only use dot property access for property names that are valid identifiers, i.e. something you might use for a variable name. BTW, what you're doing isn't parsing, that has happened already. You're just trying to access an object's properties, the JSON part is irrelevant. – RobG Commented Jun 28, 2019 at 5:17
Add a ment  | 

1 Answer 1

Reset to default 7

Take a look at Property Accessors. Consider the following:

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./apex/ResponseReceiver",
      "InResponseTo": "Azure_9849028456"
    }
  }
};


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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745094055a4610865.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信