我尝试解析从url响应中检索到的以下json。但是我得到了getJSONObject()的错误。解析这个json的任何帮助都表示赞赏。
{
"results": {
"5": {
"files": [
"http://localhost:8080/files/31_data_cite.png",
"http://localhost:8080/files/32_sesardm.png"
],
"details": "TEST1",
"type": "request",
"date": "2015-10-24 13:02:45.000",
"user": "abc@yahoo.com"
},
"7": {
"files": [
"http://localhost:8080/files/31_data_cite.png",
"http://localhost:8080/files/32_sesardm.png",
"http://localhost:8080/files/30_mds-db.png"
],
"details": "TEST2",
"type": "userexperience",
"date": "2015-10-24 13:07:32.000",
"user": "s@yhoo.com"
}
}
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var arr= JSON.parse(xhr.responseText);
//var str = JSON.stringify(xhr.responseText, null, 2);
//alert(str);
var obj = $.parseJSON(xhr.responseText);
for (var key in obj.results) {
var values = obj.results[key];
for (var v in values) { //v - files, details
if(v=="files")
{
var files = values.files;
for (i = 0; i < files.length; i++) {
document.write(" "+ files[i]);
}
}
document.write( " "+values.details+" "+values.type+" "values.user +"<br>");
}
}
}
答案 0 :(得分:0)
var obj = JSON.parse(xhr.responseText);
// var str = JSON.stringify(xhr.responseText, null, 2);
for ( var key in obj.results) {
document.write("Key: " + key + "<br>");
var values = obj.results[key];
if (typeof values === 'object') {
for (var i = 0; i < values.files.length; i++){
document.write("FILES: " + values.files[i]
+ "<br>");
}
}
document.write("DATA: " + values.details + " "
+ values.type + " " + values.user + "<br>");
}