views:

36

answers:

2

Hello all

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.

+1  A: 

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.

sje397
+1  A: 
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

Reigel