2011-07-29 89 views
13

我在处理中的JavaScript JSON数据,特别是在关于使用该数据作为一个数组和访问,并通过单独的值迭代的问题。JSON来JavaScript数组

{ 
    "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数组,以便我可以通过它进行迭代,为了拔出值为每个位置如下:

JSON文件的结构如下
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内拿起“类型”,而不仅仅是值:

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

任何帮助,建议,意见或更正,将不胜感激。

回答

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

迭代通过bindings,并把属性place.valuelat.value,从每个元素到一个数组long.valuepage.value,那么这个阵列添加到locations

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

+0

唉唉......如此简单感谢 –

+0

这么多忘看到我们推(ER)的时候,我们需要一个修复 –

0

你可以做这样的事情

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

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

} 
5

一个纯JavaScript非常相似的接受的答案(我喜欢)

我喜欢用负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]); 
}; 

这里是一个小提琴证明:http://jsfiddle.net/MarkSchultheiss/JH7LR/

编辑:更新的例子小提琴粘的东西,在一个div。 (使用一个小的jQuery这是不是OP问题的一部分,所以它是“补充材料”采用假设你有一个<div id='holdem'></div>地方。

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

一些有趣的我更新了小提琴显示建筑物的名称链接到网页建设。! http://jsfiddle.net/MarkSchultheiss/JH7LR/3/

2
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=[]; 
} 

凡JSON等于JSON对象