如何在包含url作为名称的javascript中解析Json数据

如何在包含url作为名称的javascript中解析Json数据,javascript,html,json,parsing,dbpedia,Javascript,Html,Json,Parsing,Dbpedia,我无法解析从此url获取的json文件 我需要从上面URL显示的json数据中找到populationTotal、areaTotal、populationDensity字段 这是从url获取的json数据片段。 例如 如何获取Json数据并使用Javascript将其输出。这对您有帮助吗 var populationTotalData=[]; for(var key in data) { if(key.match(/populationTotal/)) // or simplie

我无法解析从此url获取的json文件

我需要从上面URL显示的json数据中找到populationTotal、areaTotal、populationDensity字段

这是从url获取的json数据片段。 例如

如何获取Json数据并使用Javascript将其输出。

这对您有帮助吗

var populationTotalData=[];
for(var key in data) {
    if(key.match(/populationTotal/))
    // or simplier: if(key.indexOf("populationTotal")>-1)
        populationTotalData.push(data[key]);
}
由于存在错误,通常无法使用JavaScript直接获取数据。然而,事实证明dbpedia.org支持JSON-P,因此您可以使用如下直接JavaScript获取数据:

// This is the "callback" function. The 'data' variable will contain the JSON.
// You can access it like I have done in the alert below.
mycallback = function(data){
    alert(data["http://fr.dbpedia.org/resource/Los_Angeles"]["http://www.w3.org/2002/07/owl#sameAs"][0].value);
};

// This is the function we use to fetch the JSON data:
function requestServerCall(url) {
    var head = document.head;
    var script = document.createElement("script");

    script.setAttribute("src", url);
    head.appendChild(script);
    head.removeChild(script);
}

// Note the query string that I have added (?callback=mycallback).
// This is the name of the function that will be called with the JSON data.
requestServerCall('http://dbpedia.org/data/Los_Angeles.json?callback=mycallback');
<?php
  $los_angeles = file_get_contents('http://dbpedia.org/data/Los_Angeles.json');
?>
<script type="text/javascript">
  var data = <?php echo $los_angeles; ?>;
  alert(data["http://fr.dbpedia.org/resource/Los_Angeles"]["http://www.w3.org/2002/07/owl#sameAs"][0].value)
</script>
可以找到更多关于JSONP的非常优秀的信息。您可以使用如下代码在
mycallback
函数内部循环。显然,您必须创建嵌套循环才能获得所需的内容,因此需要修改此代码以满足您的确切需要

<script type="text/javascript">
  for(var index in data) { 
    console.log(data[index]); 
  }
</script>

对于(数据中的var索引){
console.log(数据[索引]);
}
另一种方法是通过PHP。例如:您可以将整个页面放入如下JavaScript变量:

// This is the "callback" function. The 'data' variable will contain the JSON.
// You can access it like I have done in the alert below.
mycallback = function(data){
    alert(data["http://fr.dbpedia.org/resource/Los_Angeles"]["http://www.w3.org/2002/07/owl#sameAs"][0].value);
};

// This is the function we use to fetch the JSON data:
function requestServerCall(url) {
    var head = document.head;
    var script = document.createElement("script");

    script.setAttribute("src", url);
    head.appendChild(script);
    head.removeChild(script);
}

// Note the query string that I have added (?callback=mycallback).
// This is the name of the function that will be called with the JSON data.
requestServerCall('http://dbpedia.org/data/Los_Angeles.json?callback=mycallback');
<?php
  $los_angeles = file_get_contents('http://dbpedia.org/data/Los_Angeles.json');
?>
<script type="text/javascript">
  var data = <?php echo $los_angeles; ?>;
  alert(data["http://fr.dbpedia.org/resource/Los_Angeles"]["http://www.w3.org/2002/07/owl#sameAs"][0].value)
</script>

var数据=;
警报(数据[”http://fr.dbpedia.org/resource/Los_Angeles"]["http://www.w3.org/2002/07/owl#sameAs“][0]。值)

数据['http://dbpedia.org/ontology/populationTotal'][0]。值
,etc@Phil你能给我一个代码片段吗?那条线不是working@tanvioka我已经给出了一个经过测试的答案,演示了如何去做。如果有帮助,请告诉我。