Javascript 如何通过Javascript解析多维JSON数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10084234/
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 parse multi dimensional JSON data through Javascript
提问by swapnesh
How could i parse this type of json data, getting in "results" to fetch single values like zipcode, state etc
我如何解析这种类型的 json 数据,获取“结果”以获取单个值,如邮政编码、状态等
{
"row": [
{
"id": "5",
"name": "test",
"email": "[email protected]",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}?
回答by Joseph
first, you need to parse that string with JSON.parse
首先,您需要使用JSON.parse解析该字符串
var myJson = JSON.parse(the_raw_data_string);
it ends up into an object like this:
它最终变成了这样的对象:
var myJson = {
"row": [
{
"id": "5",
"name": "test",
"email": "[email protected]",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}?
accessing the items:
访问项目:
myJson.row[0].id
myJson.row[0].name
myJson.row[0].street
//and so on...
回答by Jayanga
you can take the json result to a var like follows
您可以将 json 结果转换为如下所示的 var
var json = {
"row": [
{
"id": "5",
"name": "test",
"email": "[email protected]",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}?
then get the result to another
然后得到另一个结果
var result = json.row;
then you can iterate through the result
然后你可以遍历结果
for (var i = 0; i < result.length; i++) {
var object = result[i];
for (property in object) {
var value = object[property];
alert(property + "=" + value); // This alerts "id=5", etc..
}
}
hope this will help you
希望能帮到你
回答by Seb T.
Again here jQuery is your good friend
jQuery 再次成为你的好朋友
I have posted a sample using jsfiddlewith multiple records in your data row
我已经使用 jsfiddle发布了一个示例,在您的数据行中有多个记录
$(document).ready(function () {
var result = {
"row": [
{
"id": "5",
"name": "test",
"email": "[email protected]",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"
},
{
"id": "10",
"name": "test2",
"email": "[email protected]",
"street": "mystreet2",
"city": "mycity2",
"state": "mystate2",
"zipcode": "7891011",
"myimage": "image.gif"
}
]
};
var oE = $("#output");
$.each(result.row, function(index, value) {
//- extract target value like zipCode
oE.append($("<li></li>").text(value.zipcode));
});
});
?
Hope this helps.
希望这可以帮助。
回答by chandoo
If the json data is rawthen use json.parse.After that to loop the multi dimensional json data.
如果 json 数据是原始数据,则使用json.parse。之后循环多维json数据。
data = {"employees":[
{ "firstName":"Anushka", "lastName":"shetty" },
{ "firstName":"Shreya", "lastName":"Saran" },
{ "firstName":"Kajal", "lastName":"Agarwal" }
]};
for (var key in data.employees) {
alert(data.employees[key].firstName) //alert Anushka, Shreya, Kajal
}
回答by Yoong Kim
You can use JQuery each function:
您可以使用 JQuery 的每个功能:
$.each(myData.row, function(index,item) {
// here you can extract the data
alert (item.zipcode);
});
回答by Bergi
Use JSON.parse(jsonstring)
. Then iterate over the objects/arrays.
使用JSON.parse(jsonstring)
. 然后迭代对象/数组。