0

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":{

   },
   "SOAP-ENV:Envelope":{
      "@attributes":"....."
   },
   "SOAP-ENV:Body":{
      "@attributes":{
         "bill":{
            "customerDetil":{
               "customerFirstName":{
                  "#text":"Mahes"
               }
            }
         }
      }
   }
}

This is what I am doin in javascript:

var jsonText = xmlToJson(xmlhttp.responseXML);
             var myjson = JSON.stringify(jsonText);
                alert("JSON"+myjson);

function xmlToJson(xml) {
      // Create the return object;
      var obj = {};

      if (xml.nodeType == 1) {

        if (xml.attributes.length > 0) {
        obj["@attributes"] = {};
          for (var j = 0; j < xml.attributes.length; j++) {
            var attribute = xml.attributes.item(j);
            obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
          }
        }
      } else if (xml.nodeType == 3) {
        obj = xml.nodeValue;
      }

      if (xml.hasChildNodes()) {
        for(var i = 0; i < xml.childNodes.length; i++) {
          var item = xml.childNodes.item(i);
          var nodeName = item.nodeName;
          if (typeof(obj[nodeName]) == "undefined") {
            obj[nodeName] = xmlToJson(item);
          } else {
            if (typeof(obj[nodeName].length) == "undefined") {
              var old = obj[nodeName];
              obj[nodeName] = [];
              obj[nodeName].push(old);
            }
            obj[nodeName].push(xmlToJson(item));
          }
        }
      }
      return obj;
    };

Please tell me how to retrieve each value from my JSON string in javascript. For example CustomerFirstName.

Thanks, narayanan

4
  • 1
    You know, it would be much easier to simply format the JSON object and then use the code tag. Commented Mar 19, 2012 at 9:09
  • 5
    With due respect, you've asked twenty-seven previous questions and you've been using the site for eight months, you really should be formatting code and markup correctly by now. Not doing so is just disrespectful to the community. There is all sorts of information on the Ask a Question page about how to format things. Commented Mar 19, 2012 at 9:10
  • Sorry for the late response in formatting the code Commented Mar 19, 2012 at 9:14
  • @NarayananS: You can use this tool to format and validate JSON code: jsonformatter.curiousconcept.com Commented Mar 19, 2012 at 9:17

3 Answers 3

4

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';
Sign up to request clarification or add additional context in comments.

Comments

1

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:

var value = 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"];

2 Comments

Thanks for all but this is not working in my case.I will update my code for further info what I am doin
anybody having any solution for this?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.