如何从javascript访问此数组中的裁剪数组?我可能有很多ids
[{"id":"1","crop":"[{\"class\":\"org.project.Crop\",\"id\":6,\"commonName\":\"Wheat\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}, {\"class\":\"org.project.Crop\",\"id\":5,\"commonName\":\"Sugarcane\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}]]
答案 0 :(得分:0)
也许使用JSON.parse(a["crop"]);
另请查看this thread。
答案 1 :(得分:0)
你可以这样做:
var x = [{"id":"1","crop":"[{\"class\":\"org.project.Crop\",\"id\":6,\"commonName\":\"Wheat\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}, {\"class\":\"org.project.Crop\",\"id\":5,\"commonName\":\"Sugarcane\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}]];
var crop = JSON.parse(x[0].crop);
答案 2 :(得分:0)
您的语法错误,您最后遗漏了"
和}
这里是更正后的代码:
var data = [{"id":"1","crop":"[{\"class\":\"org.project.Crop\",\"id\":6,\"commonName\":\"Wheat\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}, {\"class\":\"org.project.Crop\",\"id\":5,\"commonName\":\"Sugarcane\",\"description\":\"N/A\",\"diseases\":[],\"growing\":\"N/A\",\"harvest\":\"N/A\"}]"}];
var crop = JSON.parse(data[0].crop);
alert(crop);
在线版:http://jsfiddle.net/w38Ku/1/
你必须解析“crop”才能将它用作对象,因为你已将它存储为字符串,而不是json对象