2017-04-06 11 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です。ここで私はjsonlintを試みたが、ファイルは私が推測する大きすぎる

{ 
    "_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 
} 

最初の数行があります。 var_dumpは、構文エラー、不正な形式のJSONを示唆しています。私は許可されていないので、イメージを投稿することはできません。

jsonの不正な形式はどこにありますか?

+1

です。 – GordonM

+1

'} {'は無効です.Jsonスニペットを連結してそのファイルを作成したようです。 – YvesLeBorg

+0

ありがとうございます。はい、私はjsonファイルが正しくないことを認識しました。私はopenweathermap.orgのウェブサイトからダウンロードしましたが、作成していません。私は今これを共有するもう少し作業をしました。まずjsonをインデックスとしてスニペット番号の配列に分割する必要がありました。本当にあなたの返信に感謝します。 – MSangha

答えて

1

このサイトはあなたを助けることができる:http://jsonlint.com/

を提供する例では、あなたがやったようにあなたがちょうど2つのオブジェクトを渡すことはできません。それを配列の中に配置する必要があります。

[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から自分でそれを生成しているときに問題になることがあります。

[{ 
    "_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:あなたは有効なJSONをチェックする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} 
私は、ユーザーが
 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ファイルが実際に正しくありませんでした入る$市内変数を使用して「_id」をフェッチを考えていた

、私はstr_replace()を使用して書かなければなりませんでしたfile_put_contents()を使用して新しい有効なjsonファイルを作成します。

あなたが投稿したものを、あなたの手に方法1以下の構文エラーを持っているように全体のファイルであればこれは私の作業コード

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!"; 
    } 
} 
関連する問題