php - json file malformed - how to find out where? -
i'm developing website using weatherapi. intend fetch weather using cityid in citylist.json file. users wil enter city name, hoping extract cityid json , use cityid fetch weather using api. there error in 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 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); } the json around 12mb. here first few lines
{ "_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 } i've tried jsonlint file big guess. var_dump suggests syntax error, malformed json. cannot post image i'm not allowed.
how can find out json malformed?
this site can you: http://jsonlint.com/
in example provided can't pass 2 objects did. need put inside array.
[obj,obj2] fixed:
[{ "_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 }]
Comments
Post a Comment