【发布时间】:2015-08-13 02:02:32
【问题描述】:
"street": {
"Group1": [
{
"poles": [
{
"ID": "001",
"DCU_ID": "B123",
"image": "lights/l1.jpg",
"lat": "23.185349646466175",
"lng": "72.62419939041138",
"lights": [
{
"light_ID": "101",
"status": "working"
},
{
"light_ID": "102",
"status": "not_working"
}
]
},
{
"ID": "002",
"DCU_ID": "B124",
"image": "lights/l2.jpg",
"lat": "23.185398958120906",
"lng": "72.62500405311584",
"lights": [
{
"light_ID": "103",
"status": "problem"
},
{
"light_ID": "104",
"status": "problem"
}
]
}
]
}
],
"Group2": [
{
"ID": "001",
"DCU_ID": "B123",
"image": "lights/l1.jpg",
"lat": "23.18610904393339",
"lng": "72.62963891029358",
"lights": [
{
"light_ID": "124",
"status": "working"
},
{
"light_ID": "125",
"status": "problem"
}
]
}
]
}
这是我的 json 文件。我想将每个极点的详细信息存储为 arr[group][poles_details]。我在 json 文件中遇到错误。 这是我的 html 文件。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>json</title>
</head>
<body onload="initialize()">
<script type="text/javascript">
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'data.json', true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function initialize() {
console.log("hi");
loadJSON(function(response) {
// Parse JSON string into object
var actual_JSON = JSON.parse(response);
alert(response[0].street[0]);
});
}
</script>
</body>
</html>
我得到的错误:未定义街道并找到非空白字符。
【问题讨论】:
-
console.log(actual_JSON) 时对象是什么样子的?
-
正如发布的那样,这是 无效 JSON。
-
@KevinF 这是我在控制台上得到的,但是在按照另一个答案中的建议进行更改后 - Object { street: Object, Group2: Array[1] }
标签: javascript json html multidimensional-array