我在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" }
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将其转换为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']
];
Run Code Online (Sandbox Code Playgroud)
有没有人对如何实现这一点有任何建议?我尝试了以下内容,但它在JSON中选择了"type",而不仅仅是值:
$.each(JSONObject.results.bindings, function(i, object) {
$.each(object, function(property, object) {
$.each(object, function(property, value) {
value;
});
});
});
Run Code Online (Sandbox Code Playgroud)
任何帮助,建议,建议或更正将不胜感激.
pim*_*vdb 30
var locations = [];
$.each(JSONObject.results.bindings, function(i, obj) {
locations.push([obj.place.value, obj.lat.value, obj.long.value, obj.page.value]);
});
Run Code Online (Sandbox Code Playgroud)
遍历bindings,并把属性place.value,lat.value,long.value并page.value从每个元素到一个数组,那么这个阵列添加到locations.
您当前的代码使用object两次,以及property因此覆盖这些变量.您应该在嵌套循环中使用唯一变量名来区分它们.
一个纯粹的Javascript非常类似于接受的答案(我喜欢)
当我有一个定义的长度时,我喜欢使用负循环来获得速度(超过传统的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]);
};
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴演示:http://jsfiddle.net/MarkSchultheiss/JH7LR/
编辑:更新了示例小提琴,将东西粘在div中.(使用一个不属于OP问题的小jQuery,所以它是"添加材料"假设你有一个<div id='holdem'></div>地方.
$(locations).each(function(i) {
$('#holdem').prepend("<div>" + $(this)[0] + " Is at:" + this + "</div>");
});
Run Code Online (Sandbox Code Playgroud)
为了一些乐趣,我更新了小提琴,将建筑物名称显示为建筑页面的链接:http: //jsfiddle.net/MarkSchultheiss/JH7LR/3/
| 归档时间: |
|
| 查看次数: |
71096 次 |
| 最近记录: |