简体   繁体   English

如何遍历jQuery中的JSON嵌套数组?

[英]How to iterate through nested array of JSON in jQuery?

I have a data in json format getting from PHP script the data is coming in following format as follows: 我有一个json格式的数据从PHP脚本获取,数据以以下格式显示,如下所示:

[{
"type":"checkbox",
"grid-name":"Sports",
"values":["Cricket","Football"],
 "input":[{"Cricket":2},{"Football":1}]
},
{"type":"checkbox",
"grid-name":"Hobbies",
"values":["Playing Chess","Swimming"],
"input":[{"Playing Chess":1},{"Swimming":2}]
},
{"type":"radiobutton",
"grid-name":"Gender",
 "values":["Male","Female"],
"input":[{"Male":3},{"Female":0}]
},
{"type":"radiobutton",
"grid-name":"Citizen",
"values":["Indian","NRI"],
"input":[{"Indian":3},{"NRI":0}]
},
{"type":"number",
"grid-name":"Age",
"input":["24","23","23"]
},
 {"type":"select",
"grid-name":"City",
"values":["Satara","New york","Korea"],
"input":[{"Satara":1},{"New york":1},{"Korea":1}]
}]

i want to capture the values & input array. 我想捕获值和输入数组。 How to access through nested array? 如何通过嵌套数组访问?

jQuery: jQuery的:

$.each(yourObject, function( index, value ) {
    console.log(value.values);
    console.log(value.input);
});

Native js ( but better don't use it, accordingly to this ): 本地JS( 但最好不要使用它,因此以这个 ):

for (index in yourObject) {
    console.log(yourObject[index].values);
    console.log(yourObject[index].input);
}

Native js, another example: 本机js,另一个示例:

for (var i = 0; i < yourObject.length; i++) {
    console.log(yourObject[i].values);
    console.log(yourObject[i].input);
}

You're looking for $.each . 您正在寻找$.each This will allow you to loop through object-arrays. 这将允许您遍历对象数组。

https://api.jquery.com/jquery.each/ https://api.jquery.com/jquery.each/

If you're also asking how to capture the values and you have the raw text, you're also looking for $.parseJSON 如果您还询问如何捕获值并且拥有原始文本,那么您也在寻找$.parseJSON

https://api.jquery.com/jquery.parsejson/ https://api.jquery.com/jquery.parsejson/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM