简体   繁体   中英

How to retrieve data from multi-dimentional json string?

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

This is my json file. I want to store the details for every pole as arr[group][poles_details]. I am getting an error in the json file. This is my html file.

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

Errors I get: street is not defined and non-whitespace character found.

You posted invalid JSON .

The following will pass a validation (see if this what you need):

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

I made two changes:

  1. Added { at the very beginning (before "street").
  2. Removed { before "Group2".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM