如何在 javascript 中从 JSON 对象中检索值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9767401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve values from JSON object in javascript
提问by Narayanan S
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:
我有一个包含 SOAP 消息内容的 JSON 字符串。这是我的 JSON 字符串:
{
"xml":{
},
"SOAP-ENV:Envelope":{
"@attributes":"....."
},
"SOAP-ENV:Body":{
"@attributes":{
"bill":{
"customerDetil":{
"customerFirstName":{
"#text":"Mahes"
}
}
}
}
}
}
This is what I am doin in javascript:
这就是我在 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
.
请告诉我如何在 javascript 中从我的 JSON 字符串中检索每个值。例如CustomerFirstName
。
Thanks, narayanan
谢谢,纳拉亚南
回答by Hamish
Well, you either have a JSON stringor a Javascript object. There is no such thing as a "JSON object" - JSON is a string notation for encoding a Javascript Object.
好吧,您要么有一个 JSON字符串,要么有一个 Javascript对象。没有“JSON 对象”这样的东西 - JSON 是用于编码 Javascript 对象的字符串表示法。
If you have a JSON string, you need to turn it into a Javascript Object - search SO for numerous examples.
如果您有一个 JSON 字符串,则需要将其转换为 Javascript 对象 - 搜索 SO 以获取大量示例。
If you have a Javascript object, you can access attributes via the dot notation, or array notation. E.g.
如果您有一个 Javascript 对象,您可以通过点表示法或数组表示法访问属性。例如
var obj = { 'foo' : 'bar', 'foo2' : { 'foo3' : 'bar2' }};
obj.foo; // 'bar';
obj['foo']; // 'bar';
obj.foo2['foo3']; // 'bar2';
回答by Stanley Tang
Parse the JSON string first:
首先解析JSON字符串:
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:
由于 JSON 字符串只是字典/关联数组,因此您可以通过执行以下操作来获取 javascript 中的值:
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"];
回答by ravi404
Please go through json.organd json guide. This might help you