NLV
NLV

Reputation: 21641

Parsing XML-to-JSON converted object in Javascript

I've an XML object converted into the following JSON object

"{"?xml":{"@version":"1.0","@encoding":"utf-8"},"Response":{"Users":null,"Messages":{"Tell":{"Notify":{"@From":"abc","@Message":"hi system, its abc<br/>"}},"Group":null},"PersistedMessages":{"Tell":null,"Group":null}}}"

How can i get the values inside the xml nodes from this JSON object. For example, how can i get that the version is 1.0 from the @version attribute?

Thank you.

Upvotes: 0

Views: 405

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

var json = {
    "?xml": {
        "@version": "1.0",
        "@encoding": "utf-8"
    },
    "Response": {
        "Users": null,
        "Messages": {
            "Tell": {
                "Notify": {
                    "@From": "abc",
                    "@Message": "hi system, its abc<br/>"
                }
            },
            "Group": null
        },
        "PersistedMessages": {
            "Tell": null,
            "Group": null
        }
    }
}
    alert(json["?xml"]["@version"]);
​

demo

Upvotes: 1

sje397
sje397

Reputation: 41812

To get the version, once you have the JSON object (lets call it xobj), use:

xobj['?xml']['@version']

When a javascript object has properties that are cannot be referenced using the '.' operator (because they don't conform to the variable naming rules) like your properties above, you can access the properties using the [''] method.

Upvotes: 2

Related Questions