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
codetag.