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

How to retrieve data from multi-dimentional json string?

本文关键字:字符串 检索 数据 json      更新时间:2023-09-26
"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>

您发布的无效 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. 在开头添加{(在"street"之前)
  2. 删除"Group2"前的{