我有XMLHttpRequest().responseText
创建的REST API字符串:
{"value":{"patternList":"ERROR,Chase,Theater Chase,Random Colors & Locations,All Colors & Random Locations,Color Wipe,Rainbow,Rainbow Cycle,Theater Chase Rainbow,Larson Scanner,Random,Demo","MQUEUE":"3,4,5,6,7,8,9,10","currentPatternName":"Theater Chase"},"response":"get"}
"值"的值属性(令人困惑,我知道)本身就是一个JSON对象。该字符串已分配给result
,var obj = JSON.parse(result)
失败。但是,相同的字符串在jsonlint.com中有效。
如果我在嵌入的JSON对象周围插入方括号:
result = request.responseText;
result = result.replace(":{", ":[{");
result = result.replace("},", "}],");
var obj = JSON.parse(result);
所有这一切都很好。带方括号的字符串也在jsonlint.com上验证。
JSON.parse()
是否有问题?我是否应该将缺少的方括号作为错误报告给API提供商。
谢谢, 凯文
答案 0 :(得分:0)
您不应该再次使用JSON.parse。
var responseText = JSON.stringify({
"value": {
"patternList": "ERROR,Chase,Theater Chase,Random Colors & Locations,All Colors & Random Locations,Color Wipe,Rainbow,Rainbow Cycle,Theater Chase Rainbow,Larson Scanner,Random,Demo",
"MQUEUE": "3,4,5,6,7,8,9,10",
"currentPatternName": "Theater Chase"
},
"response": "get"
});
var responseJSON = JSON.parse(responseText);
var value = responseJSON.value;
var patternList = value.patternList;
alert(patternList);