如何从多维json字符串中检索数据?

时间:2015-05-30 05:15:54

标签: javascript json html5 multidimensional-array

"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>

我得到的错误:未定义street并找到非空白字符。

1 个答案:

答案 0 :(得分:0)

您发布了无效 JSON

以下内容将通过验证(请参阅以下内容):

{
    "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"
                }
            ]
        }
    ]
}

我做了两处修改:

  1. 在开头添加{(在“街道”之前)。
  2. 在“Group2”之前删除了{