JSON 到 JavaScript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6872832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:34:54  来源:igfitidea点击:

JSON to javaScript array

javascriptjson

提问by saturn_research

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:

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

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

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:

我希望能够将其转换为 JavaScript 数组,如下所示,以便我可以遍历它并按顺序提取每个位置的值:

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']
];

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:

有没有人对如何实现这一目标有任何建议?我尝试了以下方法,但它在 JSON 中提取了“类型”,而不仅仅是值:

$.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.

任何帮助、建议、建议或更正将不胜感激。

回答by pimvdb

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

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

迭代通过bindings,并把该属性place.valuelat.valuelong.valuepage.value从每个元素到一个数组,再加入此数组locations

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

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

回答by Mark Schultheiss

for a pure Javascript very similar to the accepted answer (which I like)

对于与接受的答案非常相似的纯 Javascript(我喜欢)

I like to use a negative while loop for speed (over a traditional for loop) when I have a defined length. This is likely faster than the jQuery answer also.

当我有一个定义的长度时,我喜欢使用负的 while 循环来提高速度(而不是传统的 for 循环)。这也可能比 jQuery 答案更快。

var i = JSONObject.results.bindings.length;
var locations = [];
while (i--) {
    t = JSONObject.results.bindings[i];
    locations.push([t.place.value, t.lat.value, t.long.value, t.page.value]);
};
//now show the places
var c = locations.length;
while (c--) {
    alert(locations[c][0]);
};

Here is a fiddle to demonstrate: http://jsfiddle.net/MarkSchultheiss/JH7LR/

这是一个演示的小提琴:http: //jsfiddle.net/MarkSchultheiss/JH7LR/

EDIT: Updated the example fiddle to stick the stuff in a div. (uses a little jQuery which was not part of the OP question so it is "added material" makes an assumption you have a <div id='holdem'></div>somewhere.

编辑:更新示例小提琴以将内容粘贴在 div 中。(使用一个不属于 OP 问题的小 jQuery,因此它是“添加材料”,假设您有<div id='holdem'></div>某个地方。

$(locations).each(function(i) {
    $('#holdem').prepend("<div>" + $(this)[0] + " Is at:" + this + "</div>");
});

For some fun I updated the fiddle to show the building name as a link to the building page: http://jsfiddle.net/MarkSchultheiss/JH7LR/3/

为了一些乐趣,我更新了小提琴以将建筑物名称显示为建筑物页面的链接:http: //jsfiddle.net/MarkSchultheiss/JH7LR/3/

回答by sarath joseph

loc=json.results.bindings;
tempar1=[];
tempar2=[];

for (i=0;i<loc.length;i++) {     
    for (prop in loc[i]) {
        temp=loc[i][prop].value;
        tempar1.push(temp);
    }
    tempar2.push(tempar1);
    tempar1=[];
}

Where json is equal to the Json Object

其中 json 等于 Json 对象

回答by T.CK

You don't need jQuery for this.

为此,您不需要 jQuery。

var locations = [];
JSONObject.results.bindings.forEach(function(binding) {
    locations.push([
        binding.place.value,
        binding.lat.value,
        binding.long.value,
        binding.page.value
    ]);
});

回答by T9b

you can do something like this

你可以做这样的事情

for (i=0;i<JSONObject.results.bindings.length;i++)

{newObject = JSONObject.results.bindings;
somethingelse = newObject[i].place.type;

}