Skip to content Skip to sidebar Skip to footer

How To Retrieve Values From Json Object In Javascript

Possible Duplicate: How can i get values from json and display them in javascript I have a JSON String which will contain SOAP Message content. This is my JSON string: { 'xml

Solution 1:

Well, you either have a JSON string or a Javascript object. There is no such thing as a "JSON object" - JSON is a string notation for encoding a Javascript Object.

If you have a JSON string, you need to turn it into a Javascript Object - search SO for numerous examples.

If you have a Javascript object, you can access attributes via the dot notation, or array notation. E.g.

var obj = { 'foo' : 'bar', 'foo2' : { 'foo3' : 'bar2' }};

obj.foo; //'bar';
obj['foo']; //'bar';
obj.foo2['foo3']; //'bar2';

Solution 2:

Parse the JSON string first:

var msgs = JSON.parse(json);

Since JSON strings are simply dictionaries/associative arrays, you can just get the values in javascript by doing something like:

varvalue = msgs["key"];

In your case, it seems like the value is nested inside multiple dictionaries, so perhaps something like:

var customerName = msgs["SOAP-ENV:Body"]["@attributes"]["bill"]["customerDetil"]["customerFirstName"];

Solution 3:

Please go through json.org and json guide. This might help you

Post a Comment for "How To Retrieve Values From Json Object In Javascript"