JSON到javaScript数组

JSON到javaScript数组

本文介绍了JSON到javaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中处理JSON数据时遇到问题,特别是在将数据用作数组以及访问和迭代单个值方面。 JSON文件的结构如下:

I'm having a problem handling JSON data within JavaScript, specifically in regards to using the data as an array and accessing and iterating through individual values. The JSON file is structured as follows:

{
  "head": {
    "vars": [ "place" , "lat" , "long" , "page" ]
  } ,
  "results": {
    "bindings": [
      {
        "place": { "type": "literal" , "value": "Building A" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "10.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-1.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/a.html" }
      } ,
      {
        "place": { "type": "literal" , "value": "Building B" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "11.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-2.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/b.html" }
      } ,
      {
        "place": { "type": "literal" , "value": "Building C" } ,
        "lat": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "12.3456" } ,
        "long": { "datatype": "http://www.w3.org/2001/XMLSchema#float" , "type": "typed-literal" , "value": "-3.2345" } ,
        "page": { "type": "uri" , "value": "http://www.example.com/c.html" }
      }
    ]
  }
}

我想成为能够将其转换为JavaScript数组,如下所示,以便我可以迭代它并按顺序拉出每个位置的值:

I want to be able to convert this into a JavaScript array as follows in order that I can iterate through it and pull out the values for each location in order:

var locations = [
        ['Building A',10.3456,-1.2345,'http://www.example.com/a.html'],
        ['Building B',11.3456,-2.2345,'http://www.example.com/b.html'],
        ['Building C',12.3456,-3.2345,'http://www.example.com/c.html']
];

有人对如何实现这一点有任何建议吗?我尝试过以下方法,但它会在JSON中选择type,而不仅仅是值:

Does anyone have any advice on how to achieve this? I have tried the following, but it picks up the "type" within the JSON, rather than just the value:

$.each(JSONObject.results.bindings, function(i, object) {
    $.each(object, function(property, object) {
        $.each(object, function(property, value) {
              value;
        });
    });
});

我们非常感谢任何帮助,建议,建议或更正。

Any help, suggestions, advice or corrections would be greatly appreciated.

推荐答案

var locations = [];
$.each(JSONObject.results.bindings, function(i, obj) {
    locations.push([obj.place.value, obj.lat.value, obj.long.value, obj.page.value]);
});

迭代绑定,然后输入属性 place.value lat.value long.value page.value 从每个元素到一个数组,然后将此数组添加到 locations

Iterate through bindings, and put the properties place.value, lat.value, long.value and page.value from each element into an array, then add this array to locations.

您当前的代码使用 object 两次,以及属性,从而覆盖这些变量。您应该在嵌套循环中使用唯一变量名来区分它们。

Your current code uses object twice, as well as property, thus overwriting those variables. You should use unique variable names in nested loops to be able to distinguish between them.

这篇关于JSON到javaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:35