【问题标题】:How to retrieve data from multi-dimentional json string?如何从多维json字符串中检索数据?
【发布时间】: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


【解决方案1】:

您发布了无效 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”之前的{

【讨论】:

  • 当我使用这个时,** alert(actual_JSON.street[0]);** 我得到 undefined 作为输出。
  • street 不是数组,因此 street[0] 确实是未定义的。 Group1Group2 是数组..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多