我需要将所有json对象和帧循环到html中。我可以迭代所有json对象,但我不能只获得一个json对象(第一个json对象)。
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
obj = JSON.parse(res);
console.log('response length:' + obj.length);
for (var i = 0; i < obj.length; i++) {
var finalResult = "";
var objects = obj[i];
for (var key in objects) {
var res = "<tr><td>" + objects.ID + "</td><td>" + objects.mobile + "</td><td>" + objects.feedback + "</td><td></tr>";
console.log('res:' + res);
finalResult = res.concat(res);
console.log('finalResult:' + finalResult);
}
}
我无法将所有内容都放入'tr'元素,因为javascript doest没有stringbuffer。我认为可以在java中使用StringBuffer。如何使用javascript / jquery完成?
请帮助我。
答案 0 :(得分:2)
您正在代码中间重新定义obj:
var obj = obj[i];
使用其他名称:
你也在循环中重置你的finalResult(并且不需要内部循环):
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
var phones = JSON.parse(res);
console.log('response length:' + phones.length);
var finalResult = "";
for (var i = 0; i < phones.length; i++) {
var str1 = "aasd";
var obj = phones[i];
var res = "<tr><td>" + obj.ID + "</td><td>" + obj.mobile + "</td><td>" + obj.feedback + "</td><td></tr>";
console.log('res:' + res);
finalResult += res;
}
console.log('finalResult:' + finalResult);
$('#result').append(finalResult);
你可以使用纯粹的jQuery做同样的事情,但是@Rory McCrossan已经发布了一个很好的版本,所以我不打算在这里添加一个。
答案 1 :(得分:2)
问题是因为您正在重新定义您在迭代函数中循环的变量obj
。
正如您使用jQuery对此进行了标记,因此使用$.each
来构建表格是一个较短的替代方法:
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
var phones = JSON.parse(res);
$.each(phones, function(i, obj) {
$('<tr />')
.append('<td>' + obj.ID + '</td><td>' + obj.mobile + '</td><td>' + obj.feedback + '</td><td>')
.appendTo('table');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table></table>
&#13;
答案 2 :(得分:2)
您不需要内部for循环,您可以直接访问您的元素。我还做了一些其他的改动,可以在下面找到:
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
obj = JSON.parse(res);
var finalResult = "";
console.log('response length:' + obj.length);
for (var i = 0; i < obj.length; i++) {
var str1 = "aasd";
var tableRow = "<tr><td>" + obj[i]["ID"] + "</td><td>" + obj[i]["mobile"] + "</td><td>" + obj[i]["feedback"] + "</td><td></tr>";
finalResult += tableRow;
}
console.log('finalResult:' + finalResult);
我希望你觉得它很有帮助。
答案 3 :(得分:1)
使用jquery你可以做的每种方法
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
obj = JSON.parse(res);
var finalResult = "";
$.each(obj, function(i, item) {
var res = "<tr><td>" + item.ID + "</td><td>" + item.mobile + "</td><td>" + item.feedback + "</td><td></tr>";
console.log('res:' + res);
finalResult += res;
});
$('body').append(finalResult)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 4 :(得分:1)
请参阅此代码
var jsonobj = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
obj = JSON.parse(jsonobj);
alert(obj);
$.each(obj, function (index, item) {
$('<tr/>')
.append('<td>' + item.ID + '</td><td>' + item.mobile + '</td><td>' + item.feedback + '</td><td>')
.appendTo('table');
});
答案 5 :(得分:1)
我想你想这样做:
var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback goes here"},{"ID":"1485","mobile":"Moto","feedback":"feedback goes here"},{"ID":"6982","mobile":"iPhone","feedback":"feedback goes here"}]';
obj = JSON.parse(res);
console.log('response length:' + obj.length);
var finalResult = "";
for (var i = 0; i < obj.length; i++) {
var str1 = "aasd";
var obj1 = obj[i];
for (var key in obj1) {
var res = "<tr><td>" + obj1.ID + "</td><td>" + obj1.mobile + "</td><td>" + obj1.feedback + "</td><td></tr>";
//console.log('res:' + res);
finalResult += res;
}
}
console.log('finalResult:' + finalResult);