以下是我的控制器,它返回String
@RequestMapping("/hello1")
public @ResponseBody
String hello1() {
String jsonString = "[{\"deviceId\": \" e71b0be12218393e121cb728595af68995889fb9\",\"deviceName\": \" Xcaliber?s iPad3\",\"serialNumber\": \" DMPH9E74DJ8R\",\"modelNumber\": \" MC706\",\"productType\":\" iPad3,1\",\"deviceClass\": \" iPad\"},{\"deviceId\": \"\", \"deviceName\": \"\",\"serialNumber\": \"\",\"modelNumber\": \"\",\"productType\": \"\",\"deviceClass\": \"\"}]";
return jsonString;
}
我在javascript中将此字符串作为响应。 我正在尝试使用javascript中的代码
var deviceJsonDetails = $.parseJSON(response);
for(var count = 0;count < response.length; count++){
var id = count+1;
alert(deviceJsonDetails[count].deviceId);
$("#deviceId"+id).html(deviceJsonDetails[count].deviceId);
$("#deviceName"+id).html(deviceJsonDetails[count].deviceName);
$("#serialNumber"+id).html(deviceJsonDetails[count].serialNumber);
$("#modelNumber"+id).html(deviceJsonDetails[count].modelNumber);
}
但它在var deviceJsonDetails = $.parseJSON(response);
上出错了
错误:SyntaxError:JSON.parse:期望的属性名称或'}'
你能否告诉我在js中解析相同字符串的错误或替代解决方案在哪里?
答案 0 :(得分:2)
您需要解析的正确字符串应为:删除“\”
[
{
"deviceId": "e71b0be12218393e121cb728595af68995889fb9",
"deviceName": "Xcaliber?siPad3",
"serialNumber": "DMPH9E74DJ8R",
"modelNumber": "MC706",
"productType": "iPad3,1",
"deviceClass": "iPad"
},
{
"deviceId": "",
"deviceName": "",
"serialNumber": "",
"modelNumber": "",
"productType": "",
"deviceClass": ""
}
]