我在服务器中有一个php文件,它从db中选择一些值并创建一个JSON然后从我的html文件访问该JSON。 这是我的json
[{"id":"1","Intensity":"1","Location":"12.48,77.26"},
{"id":"2","Intensity":"2","Location":"12.47,77.26"},
{"id":"3","Intensity":"2","Location":"12.47,77.27"},
{"id":"4","Intensity":"2","Location":"12.46,77.24"},
{"id":"5","Intensity":"2","Location":"12.44,77.24"},
{"id":"6","Intensity":"2","Location":"12.44,77.28"},
{"id":"7","Intensity":"2","Location":"12.50,77.28"},
{"id":"8","Intensity":"2","Location":"12.45,77.30"},
{"id":"9","Intensity":"2","Location":"12.41,77.21"}]
并使用以下代码将“位置值”存储到javascript中的数组中,但不存储。当我使用alert打印该数组时,警报未显示。
以下代码中的数据结果是我的JSON本身。
for (i = 0; i < data.length; i++) {
alert(data[i]['id']);
var loc = data[i]['Location'].split(',');
alert(loc); //not printing
}
当我打印alert(data.length)
值为469.
答案 0 :(得分:2)
你的data
是一个json字符串,需要将其解析为json对象。
这样做
obj = JSON.parse(data);
for (i = 0; i < obj.length; i++) {
alert(obj[i]['id']);
var loc = obj[i]['Location'].split(',');
alert(loc); //not printing
}
答案 1 :(得分:1)
您需要解析数据。
data = JSON.parse(data)