我有如下的JSON文件,我想提取placeName'的valus,这是" Ince Minaret Medrese"和" Karatay,Konya"和纬度和长期值为37.8728和32.49以及" Karatay,Konya" lat的值为37.8667,long的值为32.5289
{
"head": {
"link": [],
"vars": ["placeName", "lat", "long"]
},
"results": {
"distinct": false,
"ordered": true,
"bindings": [{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Ince Minaret Medrese"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8728"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.49"
}
},
{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Karatay, Konya"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8667"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.5289"
}
}
]
}
}
我试着这样做
<p id="demo"></p>
<script>
var obj = {
"head": {
"link": [],
"vars": ["placeName", "lat", "long"]
},
"results": {
"distinct": false,
"ordered": true,
"bindings": [{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Ince Minaret Medrese"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8728"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.49"
}
},
{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Karatay, Konya"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8667"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.5289"
}
}
]
}
}
;
var myJSON = JSON.parse(obj);
document.getElementById("demo").innerHTML = myJSON.results.bindings[0].placeName.value;
</script>
但是我没有得到任何结果,我该如何从嵌套对象中提取数据呢?
答案 0 :(得分:0)
分别为:
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [0] [&#39; PLACENAME&#39;] [&#39;值&#39;]
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [1] [&#39; PLACENAME&#39;] [&#39;值&#39;]
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [1] [&#39; LAT&#39;] [&#39;值&#39;]
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [1] [&#39;长&#39;] [&#39;值&#39;]
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [0] [&#39; LAT&#39;] [&#39;值&#39;]
OBJ [&#39;结果&#39;] [&#39;绑定&#39;] [0] [&#39;长&#39;] [&#39;值&#39;]
您也可以使用点表示法(例如obj.results.bindings [1] .long.value),如果您在访问索引之前知道了索引,建议使用。
仅供参考:您不需要解析该对象,因为它已经是JSON。
function localFunc() {
const self = exports;
console.log(self.myVar);
}
exports.myVar = 'foo';
exports.myFunc = function () {
localFunc();
}
&#13;
答案 1 :(得分:0)
您的代码实际上是正确的,只需删除JSON.parse()
,因为您传递给parse方法的参数不是json字符串,它是一个实际的对象
所以按照以下方式进行。
var myJSON = obj;
它将开始工作。
答案 2 :(得分:-1)
您可以使用过滤器
var result = data.results.bindings.filter(function( obj ) {
return obj.placeName.value == "Ince Minaret Medrese";
});
参见MDN Docs on Array.prototype.filter()
var data = {
"head": {
"link": [],
"vars": ["placeName", "lat", "long"]
},
"results": {
"distinct": false,
"ordered": true,
"bindings": [{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Ince Minaret Medrese"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8728"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.49"
}
},
{
"placeName": {
"type": "literal",
"xml:lang": "en",
"value": "Karatay, Konya"
},
"lat": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "37.8667"
},
"long": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#float",
"value": "32.5289"
}
}
]
}
};
var result = data.results.bindings.filter(function( obj ) {
return obj.placeName.value == "Ince Minaret Medrese";
});
console.log(result)
&#13;