如何循环JSON
How to loop a JSON
我得到JSON通过ajax像这样
{
"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
"data": [
{
"id": 1,
"category": "default",
"question": "Staff Courtesy",
"deleted": false,
"version": 0
},
{
"id": 2,
"category": "default",
"question": "Staff Response",
"deleted": false,
"version": 0
},
{
"id": 3,
"category": "default",
"question": "Check In",
"deleted": false,
"version": 0
},
{
"id": 4,
"category": "default",
"question": "Check Out",
"deleted": false,
"version": 0
},
{
"id": 5,
"category": "default",
"question": "Travel Desk",
"deleted": false,
"version": 0
},
{
"id": 6,
"category": "default",
"question": "Door Man",
"deleted": false,
"version": 0
},
{
"id": 14,
"category": "client",
"question": "test question",
"deleted": false,
"version": 0
},
{
"id": 15,
"category": "client",
"question": "test1",
"deleted": false,
"version": 0
},
{
"id": 16,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 17,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 21,
"category": "client",
"question": "ggggg",
"deleted": false,
"version": 0
}
]
}
我想从
中获取name字段"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
,但它没有工作,我得到未定义的控制台。谁能告诉我怎么循环呢
演示小提琴
jQuery$.each(json.data, function (index, currPat) {
$(tbody).append('<tr><td><b>Question from data: ' + currPat.question + '</b></td><td><b>Names from questionTypes</b><select class="qType"></select></td></tr>');
});
var select = $('.qType');
$(json.questionTypes).each(function (index, currPat) {
$(select).append('<option>' + currPat.name + '</option>');
});
必须循环两次:一次用于获取question
和,然后用于获取name
。
好的,你可以这样做,
$(jsonObject.questionTypes).each(function(i,value){
//get value here using value.name
alert(value.name);
});
问题是你实际上是从不存在的data
中提取questionTypes
小提琴演示
data没有questionTypes
$.each(data1.questionTypes, function(index, currPat) {
alert(currPat.name);
});
在你的代码中我没有看到任何用户名属性,请尝试使用此代码。
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
试试这个
$.each(json.questionTypes, function (index, currPat) {
div=$('<div/>').text(currPat.name);
$('#jsoncontainter').append(div);
});
http://jsfiddle.net/ardeezstyle/497Tx/1/对于你的问题的第二部分,这里有一个解决方案。
$.each(json.data, function (index, currQuest) {
question=$('<div/>',{'class':'question'}).text(currQuest.question);
answer=$('<div/>',{'class':'answer'});
dropdown=$('<select/>');
$.each(json.questionTypes, function (index, currPat) {
option=$('<option/>').text(currPat.name).appendTo(dropdown);
});
answer.append(dropdown);
$('#jsoncontainter').append(question).append(answer);
});
小提琴
相关文章:
- 在循环中分配json值时,值被覆盖
- json对象中缺少对象循环
- 如何将数组循环到JSON代码中
- jquery在json数组中循环
- 如何在AngularJS-ng重复循环中将长JSON路径缩短为变量
- JSON 循环不起作用
- JSON 循环中只有 1 个结果
- Json 循环以随机化每次页面刷新
- JSON 循环遍历子对象
- 在 json 循环后推送 2 个数组
- jQuery JSON循环通过嵌套对象来创建菜单
- jQuery和JSON:循环JSON数组
- 循环中的JSON循环,用Angular渲染父元素和子元素
- Hubot咖啡脚本和JSON循环
- JSON循环对象类型恢复器
- 如何在JSON循环中添加多数组
- 获取AJAX请求和JSON循环
- JSON循环引用角度错误
- 从json循环两个数组
- Jquery读取输入字段,从JSON循环创建