Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:从json获取值_Javascript_Html_Json - Fatal编程技术网

javascript:从json获取值

javascript:从json获取值,javascript,html,json,Javascript,Html,Json,我想从这个Yahoo web服务获得的json中获得价值: 这是从该url获得的数据: { "geonames": [ { "fcodeName": "capital of a political entity", "toponymName": "Mexico City", "countrycode": "MX", "fcl": "P", "fclName": "city, village,...", "name

我想从这个Yahoo web服务获得的json中获得价值:

这是从该url获得的数据:

{
  "geonames": [
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Mexico City",
      "countrycode": "MX",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Mexiko-Stadt",
      "wikipedia": "en.wikipedia.org/wiki/Mexico_City",
      "lng": -99.12766456604,
      "fcode": "PPLC",
      "geonameId": 3530597,
      "lat": 19.428472427036,
      "population": 12294193
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Manila",
      "countrycode": "PH",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Manila",
      "wikipedia": "en.wikipedia.org/wiki/Manila",
      "lng": 120.9822,
      "fcode": "PPLC",
      "geonameId": 1701668,
      "lat": 14.6042,
      "population": 10444527
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Dhaka",
      "countrycode": "BD",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Dhaka",
      "wikipedia": "en.wikipedia.org/wiki/Dhaka",
      "lng": 90.40743827819824,
      "fcode": "PPLC",
      "geonameId": 1185241,
      "lat": 23.710395616597037,
      "population": 10356500
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Seoul",
      "countrycode": "KR",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Seoul",
      "wikipedia": "en.wikipedia.org/wiki/Seoul",
      "lng": 126.977834701538,
      "fcode": "PPLC",
      "geonameId": 1835848,
      "lat": 37.5682561388953,
      "population": 10349312
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Jakarta",
      "countrycode": "ID",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Jakarta",
      "wikipedia": "en.wikipedia.org/wiki/Jakarta",
      "lng": 106.84513092041016,
      "fcode": "PPLC",
      "geonameId": 1642911,
      "lat": -6.214623197035775,
      "population": 8540121
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Tokyo",
      "countrycode": "JP",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Tokio",
      "wikipedia": "en.wikipedia.org/wiki/Tokyo",
      "lng": 139.69171,
      "fcode": "PPLC",
      "geonameId": 1850147,
      "lat": 35.6895,
      "population": 8336599
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Taipei",
      "countrycode": "TW",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Taipeh",
      "wikipedia": "en.wikipedia.org/wiki/Taipei_Railway_Station",
      "lng": 121.531846,
      "fcode": "PPLC",
      "geonameId": 1668341,
      "lat": 25.047763,
      "population": 7871900
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Bogotá",
      "countrycode": "CO",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Bogotá",
      "wikipedia": "en.wikipedia.org/wiki/Bogot%C3%A1",
      "lng": -74.08175468444824,
      "fcode": "PPLC",
      "geonameId": 3688689,
      "lat": 4.609705849789108,
      "population": 7674366
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Beijing",
      "countrycode": "CN",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Peking",
      "wikipedia": "en.wikipedia.org/wiki/Beijing",
      "lng": 116.397228240967,
      "fcode": "PPLC",
      "geonameId": 1816670,
      "lat": 39.9074977414405,
      "population": 7480601
    },
    {
      "fcodeName": "capital of a political entity",
      "toponymName": "Hong Kong",
      "countrycode": "HK",
      "fcl": "P",
      "fclName": "city, village,...",
      "name": "Hong Kong",
      "wikipedia": "en.wikipedia.org/wiki/Hong_Kong",
      "lng": 114.157691001892,
      "fcode": "PPLC",
      "geonameId": 1819729,
      "lat": 22.2855225817732,
      "population": 7012738
    }
  ]
}
如何从这个json获取
fcodeName

我知道这对大家来说都很简单,但我是第一次使用html、javascript和jquery。我在谷歌上搜索过它,但无法了解如何使用json数据。我期待一个详细的解释。谢谢你,devs

编辑:

我试着这样做:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
    var url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";
    $.ajax({
        url: url,
        type: 'GET',
        dataType: "json",
        success: function(data){
            console.log("Success: "+data.geonames[0].fcodeName);
        },
        error:function(data){
            console.log("Error: "+data);
        }
    });
</script>
使用JSON.parse()将其转换为数据结构,然后将其从数据中取出。

如何从该JSON中获取fcodeName

你可以用

  data.geonames[0].fcodeName
由于
geonames
是一个必须使用索引进行迭代的数组


您根本没有检查如何执行此操作吗?您的代码应该与您提供的响应示例相匹配。但是,您在AJAX调用中提供的URL正在返回
{“status”:{“message”:“已超过演示每天30000学分的限制。请使用特定于应用程序的帐户。不要为您的应用程序使用演示帐户。”,“value”:18}
此处。也许这就是它不起作用的原因。你检查过你的控制台有没有错误吗?上面的代码有什么问题吗。检查我的编辑。@shre202,你的代码没有问题。您可以
console.log(数据)
。当前
geonames
未定义。谢谢你指导我。
  data.geonames[0].fcodeName