2017-04-06 333 views
1

我正在使用weatherapi开发网站。我打算使用citylist.json文件中的cityid获取天气。用户将输入城市名称,我希望从此json中提取cityid,并使用cityid使用api获取天气。但是json中有一些错误。json文件格式不正确 - 如何找出哪里?

if (file_exists('citylist.json')) { 

     $cityArray = file_get_contents("citylist.json"); 

     $cityAA = json_decode($cityArray,true); 

      switch (json_last_error()) { 
       case JSON_ERROR_NONE: 
        echo ' - No errors'; 
       break; 
       case JSON_ERROR_DEPTH: 
        echo ' - Maximum stack depth exceeded'; 
       break; 
       case JSON_ERROR_STATE_MISMATCH: 
        echo ' - Underflow or the modes mismatch'; 
       break; 
       case JSON_ERROR_CTRL_CHAR: 
        echo ' - Unexpected control character found'; 
       break; 
       case JSON_ERROR_SYNTAX: 
        echo ' - Syntax error, malformed JSON'; 
       break; 
       case JSON_ERROR_UTF8: 
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; 
       break; 
       default: 
        echo ' - Unknown error'; 
       break; 
      } 
     print_r($cityAA);    
    } 

json约12mb。这里有前几行

{ 
    "_id": 14256, 
    "coord": { 
     "lon": 48.570728, 
     "lat": 34.790878 
    }, 
    "country": "IR", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 132142 
    }, 
    "langs": [{ 
     "de": "Azad Shahr" 
    }, 
    { 
     "fa": "آزادشهر" 
    }], 
    "name": "Azadshahr", 
    "stat": { 
     "level": 1.0, 
     "population": 514102 
    }, 
    "stations": [{ 
     "id": 7030, 
     "dist": 9, 
     "kf": 1 
    }], 
    "zoom": 10 
}{ 
    "_id": 18918, 
    "coord": { 
     "lon": 34.058331, 
     "lat": 35.012501 
    }, 
    "country": "CY", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 146615 
    }, 
    "langs": [{ 
     "en": "Protaras" 
    }, 
    { 
     "ru": "Протарас" 
    }], 
    "name": "Protaras", 
    "stat": { 
     "level": 1.0, 
     "population": 20230 
    }, 
    "stations": [{ 
     "id": 5448, 
     "dist": 42, 
     "kf": 1 
    }], 
    "zoom": 6 
} 

我试过jsonlint,但文件太大,我猜。 var_dump建议语法错误,格式错误的JSON。由于我不被允许,我无法发布图片。

我如何知道json的格式不正确?

+1

如果整个文件,就像你贴什么,那么你有你的手的方式不止一个语法错误。 – GordonM

+1

'} {'无效,看起来像通过连接json片段创建了该文件。 – YvesLeBorg

+0

感谢您的回复。是的,我意识到json文件不正确。我没有创建它,虽然我从openweathermap.org网站下载它。我已经做了更多的工作,现在我将分享这些。我需要首先将json拆分为一个以snippet number作为索引的数组。我真的很感激你的回复。 – MSangha

回答

1

这个网站可以帮助你:http://jsonlint.com/

在提供的例子,你不能只是通过两个物体像你这样。你需要把它放在一个数组中。

[obj,obj2] 

修正:

[{ 
    "_id": 14256, 
    "coord": { 
     "lon": 48.570728, 
     "lat": 34.790878 
    }, 
    "country": "IR", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 132142 
    }, 
    "langs": [{ 
     "de": "Azad Shahr" 
    }, { 
     "fa": "آزادشهر" 
    }], 
    "name": "Azadshahr", 
    "stat": { 
     "level": 1.0, 
     "population": 514102 
    }, 
    "stations": [{ 
     "id": 7030, 
     "dist": 9, 
     "kf": 1 
    }], 
    "zoom": 10 
}, { 
    "_id": 18918, 
    "coord": { 
     "lon": 34.058331, 
     "lat": 35.012501 
    }, 
    "country": "CY", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 146615 
    }, 
    "langs": [{ 
     "en": "Protaras" 
    }, { 
     "ru": "Протарас" 
    }], 
    "name": "Protaras", 
    "stat": { 
     "level": 1.0, 
     "population": 20230 
    }, 
    "stations": [{ 
     "id": 5448, 
     "dist": 42, 
     "kf": 1 
    }], 
    "zoom": 6 
}] 
+0

感谢您指出错误。 – MSangha

+0

不客气。如果这个答案能帮助你解决问题,你能把它标记为正确的吗?谢谢 – Xidh

1

它看起来像该文件包含多个JSON对象,但没有很好地集成。

这是有效的JSON:

{ 
    "_id": 14256, 
    "coord": { 
     "lon": 48.570728, 
     "lat": 34.790878 
    }, 
    "country": "IR", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 132142 
    }, 
    "langs": [{ 
     "de": "Azad Shahr" 
    }, 
    { 
     "fa": "آزادشهر" 
    }], 
    "name": "Azadshahr", 
    "stat": { 
     "level": 1.0, 
     "population": 514102 
    }, 
    "stations": [{ 
     "id": 7030, 
     "dist": 9, 
     "kf": 1 
    }], 
    "zoom": 10 
} 

这是太:

{ 
    "_id": 18918, 
    "coord": { 
     "lon": 34.058331, 
     "lat": 35.012501 
    }, 
    "country": "CY", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 146615 
    }, 
    "langs": [{ 
     "en": "Protaras" 
    }, 
    { 
     "ru": "Протарас" 
    }], 
    "name": "Protaras", 
    "stat": { 
     "level": 1.0, 
     "population": 20230 
    }, 
    "stations": [{ 
     "id": 5448, 
     "dist": 42, 
     "kf": 1 
    }], 
    "zoom": 6 
} 

但他们是如何结合在一起,事实并非如此。这可能是JSON文件导入的问题,或者您是通过API自行生成的。

甲有效的JSON将被形成为像那些对象的数组:

[{ 
    "_id": 14256, 
    "coord": { 
     "lon": 48.570728, 
     "lat": 34.790878 
    }, 
    "country": "IR", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 132142 
    }, 
    "langs": [{ 
     "de": "Azad Shahr" 
    }, { 
     "fa": "آزادشهر" 
    }], 
    "name": "Azadshahr", 
    "stat": { 
     "level": 1.0, 
     "population": 514102 
    }, 
    "stations": [{ 
     "id": 7030, 
     "dist": 9, 
     "kf": 1 
    }], 
    "zoom": 10 
}, { 
    "_id": 18918, 
    "coord": { 
     "lon": 34.058331, 
     "lat": 35.012501 
    }, 
    "country": "CY", 
    "geoname": { 
     "cl": "P", 
     "code": "PPL", 
     "parent": 146615 
    }, 
    "langs": [{ 
     "en": "Protaras" 
    }, { 
     "ru": "Протарас" 
    }], 
    "name": "Protaras", 
    "stat": { 
     "level": 1.0, 
     "population": 20230 
    }, 
    "stations": [{ 
     "id": 5448, 
     "dist": 42, 
     "kf": 1 
    }], 
    "zoom": 6 
}] 

PS:可以使用http://jsonlint.com/检查有效的JSON。

0

我尝试使用下面的函数

 function json_split_objects($json){ 
       $q = FALSE; 
       $len = strlen($json); 
       for($l=$c=$i=0;$i<$len;$i++) 
       { 
        $json[$i] == '"' && ($i>0?$json[$i-1]:'') != '\\' && $q = !$q; 
        if(!$q && in_array($json[$i], array(" ", "\r", "\n", "\t"))){continue;} 
        in_array($json[$i], array('{', '[')) && !$q && $l++; 
        in_array($json[$i], array('}', ']')) && !$q && $l--; 
        (isset($objects[$c]) && $objects[$c] .= $json[$i]) || $objects[$c] = $json[$i]; 
        $c += ($l == 0); 
       } 
       return $objects; 
     } 

    if (file_exists('current_cities.json')) { 
     echo "here";  
     $cityJson = file_get_contents("current_cities.json"); 

     $city_json_data_array = json_split_objects($cityJson); 

JSON文件转换成多个JSON片段的数组但这并没有工作,下面是生成拆分数组,但它没有在代码段之间放置逗号(,)。

Array 
(
    [0] => {"_id":14256,"coord":{"lon":48.570728,"lat":34.790878},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":132142},"langs":[{"de":"Azad Shahr"},{"fa":"آزادشهر"}],"name":"Azadshahr","stat":{"level":1.0,"population":514102},"stations":[{"id":7030,"dist":9,"kf":1}],"zoom":10} 
    [1] => {"_id":18918,"coord":{"lon":34.058331,"lat":35.012501},"country":"CY","geoname":{"cl":"P","code":"PPL","parent":146615},"langs":[{"en":"Protaras"},{"ru":"Протарас"}],"name":"Protaras","stat":{"level":1.0,"population":20230},"stations":[{"id":5448,"dist":42,"kf":1}],"zoom":6} 
    [2] => {"_id":23814,"coord":{"lon":47.055302,"lat":34.383801},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":128222},"langs":[{"fa":"کهریز"}],"name":"Kahriz","stat":{"level":1.0,"population":766706},"stations":[{"id":7022,"dist":10,"kf":1}],"zoom":7} 
    [3] => {"_id":24851,"coord":{"lon":47.9725,"lat":34.073399},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":125605},"langs":[{"fa":"نور آباد"},{"link":"http://en.wikipedia.org/wiki/Nurabad%2C_Lorestan"},{"ru":"Нурабад"}],"name":"Nurabad","stat":{"level":1.0,"population":73528},"stations":[{"id":7022,"dist":80,"kf":1},{"id":7024,"dist":75,"kf":1},{"id":7073,"dist":49,"kf":1}],"zoom":9} 
    [4] => {"_id":32723,"coord":{"lon":52.309422,"lat":35.23455},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":116401},"langs":[{"fa":"ايستگاه گرمسار"}],"name":"Istgah-e Garmsar","stat":{"level":1.0,"population":49491},"stations":[{"id":7036,"dist":99,"kf":1}],"zoom":8} 
    [5] => {"_id":32767,"coord":{"lon":51.56889,"lat":35.439442},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":110791},"langs":[{"fa":"قرچك"}],"name":"Qarchak","stat":{"level":1.0,"population":251834},"stations":[{"id":7032,"dist":36,"kf":1},{"id":7074,"dist":48,"kf":1}],"zoom":9} 
    [6] => {"_id":41210,"coord":{"lon":49.195999,"lat":36.213001},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":111452},"langs":[{"fa":"خرم درّه"}],"name":"Khorram Darreh","stat":{"level":1.0,"population":50528},"stations":[{"id":7033,"dist":76,"kf":1}],"zoom":12} 
    [7] => {"_id":50672,"coord":{"lon":44.893799,"lat":2.6185},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":51966},"langs":[{"so":"Wanlaweyn"}],"name":"Wanlaweyn","stat":{"level":1.0,"population":22022},"zoom":9} 
    [8] => {"_id":52867,"coord":{"lon":44.529991,"lat":1.78784},"country":"SO","geoname":{"cl":"P","code":"PPLA2","parent":51966},"langs":[{"so":"Qoryooley"}],"name":"Qoryooley","stat":{"level":1.0,"population":51720},"zoom":8} 
    [9] => {"_id":53157,"coord":{"lon":49.872822,"lat":11.47197},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":64661},"langs":[{"link":"http://en.wikipedia.org/wiki/Qandala"},{"so":"Qandala"}],"name":"Qandala","stat":{"level":1.0,"population":15923},"zoom":8} 
    [10] => {"_id":53654,"coord":{"lon":45.34375,"lat":2.03711},"country":"SO","geoname":{"cl":"P","code":"PPLC","parent":64833},"name":"Mogadishu","stat":{"level":1.0,"population":2587183},"zoom":1} 
    [11] => {"_id":54715,"coord":{"lon":42.544498,"lat":3.79376},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":58802},"name":"Luuq","stat":{"level":1.0,"population":33820},"zoom":8} 
    [12] => {"_id":55671,"coord":{"lon":42.545361,"lat":-0.35817},"country":"SO","geoname":{"cl":"P","code":"PPLA","parent":56083},"langs":[{"de":"Kismaayo"},{"en":"Kismayo"},{"es":"Kismaayo"},{"fi":"Kismayo"},{"fr":"Kismaayo"},{"id":"Kismaayo"},{"ja":"キスマヨ"},{"link":"http://en.wikipedia.org/wiki/Kismayo"},{"nl":"Kismayo"},{"so":"Kismaayo"},{"sw":"Kismayu"}],"name":"Kismaayo","stat":{"level":1.0,"population":234852},"zoom":6} 
    [13] => {"_id":56166,"coord":{"lon":42.785351,"lat":0.48829},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":56084},"langs":[{"link":"http://en.wikipedia.org/wiki/Jilib"},{"ru":"Джилиб"}],"name":"Jilib","stat":{"level":1.0,"population":43694},"zoom":9} 
    [14] => {"_id":56335,"coord":{"lon":45.500481,"lat":2.78087},"country":"SO","geoname":{"cl":"P","code":"PPLA","parent":51967},"langs":[{"de":"Giohar"},{"en":"Giohar"},{"link":"http://en.wikipedia.org/wiki/Jowhar"},{"so":"Jawhar"}],"name":"Jawhar","stat":{"level":1.0,"population":47086},"zoom":8} 
    [15] => {"_id":56399,"coord":{"lon":42.744968,"lat":0.06968},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":56083},"langs":[{"de":"Jamaame"},{"en":"Jamaame"},{"link":"http://en.wikipedia.org/wiki/Jamame"}],"name":"Jamaame","stat":{"level":1.0,"population":185270},"zoom":6} 

我想获取的“_id”使用该用户输入

 for($i = 0, $size = count($city_json_data_array); $i < $size; ++$i) { 
      if ($city_json_data_array[$i].['name'] == $city){ 
       $cityId = $city_json_data_array[$i]['_id']; 
       print_r($cityId); 
      } 
     } 
0

所以JSON文件确实是不正确的$城市变的,我不得不使用str_replace()函数和写该文件使用file_put_contents()创建一个新的有效json文件。

这是我工作的代码

if (isset($_GET['city']) && $_GET['city']){   

    $city = ucwords($_GET['city']); 

    if (file_exists('currentCityArray.json')) { 

     $cityArray = file_get_contents("currentCityArray.json"); 
     $decodedArray=""; 
     $decodedArray = json_decode($cityArray, true); 
     //print_r($decodedArray); 
     //Search the $decodedArray to match city name entered by the user and fetch the cityID for that city 
     for($i = 0, $size = count($decodedArray);$i<$size; $i++) { 
      if ($decodedArray[$i]['name'] == $city){ 

       $cityId = $decodedArray[$i]['_id']; 

      } 
     } 

     if(!$cityId){ 

      $error .= $city." city could not be found in our database. Please enter a valid city name."; 

     }else{ 
      //Fetch the weather contents by using $cityId 

      $urlContents = file_get_contents("http://api.openweathermap.org/data/2.5/forecast?id=".urlencode($cityId)."&APPID=0934a70098e84dc720b8d7f07bb1202d"); 

      // added a flag 'true' to retrieve the data in $weatherArray as associative array 
      $weatherArray = json_decode($urlContents, true); 
      //print_r ($weatherArray); 

      if($weatherArray['cod']=="200"){ 
      //print_r($weatherArray); 

       $weatherInfo = "The weather in ".$city." is currently '".$weatherArray['list'][0]['weather'][0]['description']."'";     
       $tempCelcius = intval($weatherArray['list'][0]['main']['temp'] - 273); 
       $weatherInfo .= ". The average temperature is expected to be about: ".$tempCelcius."&deg;C";          
       $windSpeed = $weatherArray['list'][0]['wind']['speed']; 
       $weatherInfo .= ". The wind speed is currently ".$windSpeed."m/s.";       

      }else{ 

       $error .= $city." city could not be found in our database. Please enter a valid city name."; 

      } 

     }       
    }else{ 
     $error .= "Crikey ! the file with the list of cities has gone missing!"; 
    } 
}