var records = JSON.parse(JsonString);
for(var x=0;x<records.result.length;x++)
{
var record = records.result[x];
ht_text+="<b><p>"+(x+1)+" "
+record.EMPID+" "
+record.LOCNAME+" "
+record.DEPTNAME+" "
+record.CUSTNAME
+"<br/><br/><div class='slide'>"
+record.REPORT
+"</div></p></b><br/>";
}
当JsonString包含实体数组但单个实体失败时,上面的代码工作正常。结果未标识为数组! 这有什么不对吗?
答案 0 :(得分:1)
结果不是数组。你在JSON中看到任何方括号吗?你不可以。它不包含任何数组。
{"result": {"ID":"30","EMPID":"1210308550","CUSTID":"1003","STATUS":"2","DATEREPORTED":"1273234502","REPORT":"this is one more report!","NAME":"Sandeep Savarla","CUSTNAME":"Collateral","LOCID":"4","LOCNAME":"Vijayawada","DEPTNAME":"SALES"}}
当上述功能有效时,你能告诉我你的“有效”json是什么样的吗?
答案 1 :(得分:1)
在迭代之前确保它是一个数组
if ( 'undefined' == typeof records.result.length )
{
records.result = [records.result];
}
答案 2 :(得分:0)
您的代码循环遍历records.result
,就像它是一个数组一样
由于它不是数组,因此您的代码不起作用。
这个最简单的解决方案是强制它进入一个数组,如下所示:
var array = 'length' in records.result ? records.result : [ records.result ];
for(var x = 0; x < array.length; x++) {
var record = array[x];
...
答案 3 :(得分:0)
在您的代码中result
是一个对象,而不是一个数组。将它的值包含在方括号中,使其成为一个数组:
{"result": [{"ID":"30","EMPID":"1210308550","CUSTID":"1003","STATUS":"2","DATEREPORTED":"1273234502","REPORT":"this is one more report!","NAME":"Sandeep Savarla","CUSTNAME":"Collateral","LOCID":"4","LOCNAME":"Vijayawada","DEPTNAME":"SALES"}]}