I receive the below Json Data in an ajax response... I dont know how to traverse it I wana loop through it and make an array out of it, Which I will gonna be using in the HTML DOM also I wana Cache this data for No More Ajax requests if data is already available...
How Can I do that,
I have Tried to do it with $.each(data, function(key,value){ ---- }). But the problem is looping through the data I wanna put a for Loop and Inside it I wanna Populate some other array or object...
Does that Make any sense, Can we do that?
{"CountryCode":"ABW","Language":"Dutch"}{"CountryCode":"ABW","Language":"English"}{"CountryCode":"ABW","Language":"Papiamento"}{"CountryCode":"ABW","Language":"Spanish"}{"CountryCode":"AFG","Language":"Balochi"}{"CountryCode":"AFG","Language":"Dari"}{"CountryCode":"AFG","Language":"Pashto"}{"CountryCode":"AFG","Language":"Turkmenian"}{"CountryCode":"AFG","Language":"Uzbek"}{"CountryCode":"AGO","Language":"Ambo"}{"CountryCode":"AGO","Language":"Chokwe"}{"CountryCode":"AGO","Language":"Kongo"}{"CountryCode":"AGO","Language":"Luchazi"}{"CountryCode":"AGO","Language":"Luimbe-nganguela"}{"CountryCode":"AGO","Language":"Luvale"}{"CountryCode":"AGO","Language":"Mbundu"}{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"}{"CountryCode":"AGO","Language":"Ovimbundu"}{"CountryCode":"AIA","Language":"English"}{"CountryCode":"ALB","Language":"Albaniana"}{"CountryCode":"ALB","Language":"Greek"}{"CountryCode":"ALB","Language":"Macedonian"}{"CountryCode":"AND","Language":"Catalan"}{"CountryCode":"AND","Language":"French"}{"CountryCode":"AND","Language":"Portuguese"}{"CountryCode":"AND","Language":"Spanish"}{"CountryCode":"ANT","Language":"Dutch"}{"CountryCode":"ANT","Language":"English"}{"CountryCode":"ANT","Language":"Papiamento"}{"CountryCode":"ARE","Language":"Arabic"}{"CountryCode":"ARE","Language":"Hindi"}{"CountryCode":"ARG","Language":"Indian Languages"}{"CountryCode":"ARG","Language":"Italian"}{"CountryCode":"ARG","Language":"Spanish"}{"CountryCode":"ARM","Language":"Armenian"}{"CountryCode":"ARM","Language":"Azerbaijani"}{"CountryCode":"ASM","Language":"English"}{"CountryCode":"ASM","Language":"Samoan"}{"CountryCode":"ASM","Language":"Tongan"}{"CountryCode":"ATG","Language":"Creole English"}{"CountryCode":"ATG","Language":"English"}{"CountryCode":"AUS","Language":"Arabic"}{"CountryCode":"AUS","Language":"Canton Chinese"}{"CountryCode":"AUS","Language":"English"}{"CountryCode":"AUS","Language":"German"}{"CountryCode":"AUS","Language":"Greek"}{"CountryCode":"AUS","Language":"Italian"}{"CountryCode":"AUS","Language":"Serbo-Croatian"}{"CountryCode":"AUS","Language":"Vietnamese"}{"CountryCode":"AUT","Language":"Czech"}{"CountryCode":"AUT","Language":"German"}{"CountryCode":"AUT","Language":"Hungarian"}{"CountryCode":"AUT","Language":"Polish"}{"CountryCode":"AUT","Language":"Romanian"}{"CountryCode":"AUT","Language":"Serbo-Croatian"}{"CountryCode":"AUT","Language":"Slovene"}{"CountryCode":"AUT","Language":"Turkish"}{"CountryCode":"AZE","Language":"Armenian"}{"CountryCode":"AZE","Language":"Azerbaijani"}{"CountryCode":"AZE","Language":"Lezgian"}{"CountryCode":"AZE","Language":"Russian"}{"CountryCode":"BDI","Language":"French"}{"CountryCode":"BDI","Language":"Kirundi"}{"CountryCode":"BDI","Language":"Swahili"}{"CountryCode":"BEL","Language":"Arabic"}{"CountryCode":"BEL","Language":"Dutch"}{"CountryCode":"BEL","Language":"French"}{"CountryCode":"BEL","Language":"German"}{"CountryCode":"BEL","Language":"Italian"}{"CountryCode":"BEL","Language":"Turkish"}{"CountryCode":"BEN","Language":"Adja"}{"CountryCode":"BEN","Language":"Aizo"}
Your problem can be solved in two steps:
1) convert your json data to a php String
2) iterate over that array:
step 1) convert to array
$arr = json_decode($jsondata);
step 2) iterate over array
arrayRecurssive($arr);
function arrayRecurssive($arr){
foreach($arr as $key=>$val){
if(is_array($val)){
echo "$key\n";
arrayRecurssive($val);
}else{
echo "$key\n";
echo "$val\n";
}
}
}
in my example above im using '\n' as my new line, but that may as well be <br/>. further formatting of the output is left as an exercise for the coder :-)
The json data appears to be badly formatted. It should be:
[{"CountryCode":"ABW","Language":"Dutch"},{"CountryCode":"ABW","Language":"English"},{"CountryCode":"ABW","Language":"Papiamento"},{"CountryCode":"ABW","Language":"Spanish"},{"CountryCode":"AFG","Language":"Balochi"},{"CountryCode":"AFG","Language":"Dari"},{"CountryCode":"AFG","Language":"Pashto"},{"CountryCode":"AFG","Language":"Turkmenian"},{"CountryCode":"AFG","Language":"Uzbek"},{"CountryCode":"AGO","Language":"Ambo"},{"CountryCode":"AGO","Language":"Chokwe"},{"CountryCode":"AGO","Language":"Kongo"},{"CountryCode":"AGO","Language":"Luchazi"},{"CountryCode":"AGO","Language":"Luimbe-nganguela"},{"CountryCode":"AGO","Language":"Luvale"},{"CountryCode":"AGO","Language":"Mbundu"},{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"},{"CountryCode":"AGO","Language":"Ovimbundu"},{"CountryCode":"AIA","Language":"English"},{"CountryCode":"ALB","Language":"Albaniana"},{"CountryCode":"ALB","Language":"Greek"},{"CountryCode":"ALB","Language":"Macedonian"},{"CountryCode":"AND","Language":"Catalan"},{"CountryCode":"AND","Language":"French"},{"CountryCode":"AND","Language":"Portuguese"},{"CountryCode":"AND","Language":"Spanish"},{"CountryCode":"ANT","Language":"Dutch"},{"CountryCode":"ANT","Language":"English"},{"CountryCode":"ANT","Language":"Papiamento"},{"CountryCode":"ARE","Language":"Arabic"},{"CountryCode":"ARE","Language":"Hindi"},{"CountryCode":"ARG","Language":"Indian Languages"},{"CountryCode":"ARG","Language":"Italian"},{"CountryCode":"ARG","Language":"Spanish"},{"CountryCode":"ARM","Language":"Armenian"},{"CountryCode":"ARM","Language":"Azerbaijani"},{"CountryCode":"ASM","Language":"English"},{"CountryCode":"ASM","Language":"Samoan"},{"CountryCode":"ASM","Language":"Tongan"},{"CountryCode":"ATG","Language":"Creole English"},{"CountryCode":"ATG","Language":"English"},{"CountryCode":"AUS","Language":"Arabic"},{"CountryCode":"AUS","Language":"Canton Chinese"},{"CountryCode":"AUS","Language":"English"},{"CountryCode":"AUS","Language":"German"},{"CountryCode":"AUS","Language":"Greek"},{"CountryCode":"AUS","Language":"Italian"},{"CountryCode":"AUS","Language":"Serbo-Croatian"},{"CountryCode":"AUS","Language":"Vietnamese"},{"CountryCode":"AUT","Language":"Czech"},{"CountryCode":"AUT","Language":"German"},{"CountryCode":"AUT","Language":"Hungarian"},{"CountryCode":"AUT","Language":"Polish"},{"CountryCode":"AUT","Language":"Romanian"},{"CountryCode":"AUT","Language":"Serbo-Croatian"},{"CountryCode":"AUT","Language":"Slovene"},{"CountryCode":"AUT","Language":"Turkish"},{"CountryCode":"AZE","Language":"Armenian"},{"CountryCode":"AZE","Language":"Azerbaijani"},{"CountryCode":"AZE","Language":"Lezgian"},{"CountryCode":"AZE","Language":"Russian"},{"CountryCode":"BDI","Language":"French"},{"CountryCode":"BDI","Language":"Kirundi"},{"CountryCode":"BDI","Language":"Swahili"},{"CountryCode":"BEL","Language":"Arabic"},{"CountryCode":"BEL","Language":"Dutch"},{"CountryCode":"BEL","Language":"French"},{"CountryCode":"BEL","Language":"German"},{"CountryCode":"BEL","Language":"Italian"},{"CountryCode":"BEL","Language":"Turkish"},{"CountryCode":"BEN","Language":"Adja"},{"CountryCode":"BEN","Language":"Aizo"}]
You can then simply assign that to an array. For example:
var countries = $.parseJSON('[{"CountryCode":"ABW","Language":"Dutch"},{"CountryCode":"ABW","Language":"English"},{"CountryCode":"ABW","Language":"Papiamento"},{"CountryCode":"ABW","Language":"Spanish"},{"CountryCode":"AFG","Language":"Balochi"},{"CountryCode":"AFG","Language":"Dari"},{"CountryCode":"AFG","Language":"Pashto"},{"CountryCode":"AFG","Language":"Turkmenian"},{"CountryCode":"AFG","Language":"Uzbek"},{"CountryCode":"AGO","Language":"Ambo"},{"CountryCode":"AGO","Language":"Chokwe"},{"CountryCode":"AGO","Language":"Kongo"},{"CountryCode":"AGO","Language":"Luchazi"},{"CountryCode":"AGO","Language":"Luimbe-nganguela"},{"CountryCode":"AGO","Language":"Luvale"},{"CountryCode":"AGO","Language":"Mbundu"},{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"},{"CountryCode":"AGO","Language":"Ovimbundu"},{"CountryCode":"AIA","Language":"English"},{"CountryCode":"ALB","Language":"Albaniana"},{"CountryCode":"ALB","Language":"Greek"},{"CountryCode":"ALB","Language":"Macedonian"},{"CountryCode":"AND","Language":"Catalan"},{"CountryCode":"AND","Language":"French"},{"CountryCode":"AND","Language":"Portuguese"},{"CountryCode":"AND","Language":"Spanish"},{"CountryCode":"ANT","Language":"Dutch"},{"CountryCode":"ANT","Language":"English"},{"CountryCode":"ANT","Language":"Papiamento"},{"CountryCode":"ARE","Language":"Arabic"},{"CountryCode":"ARE","Language":"Hindi"},{"CountryCode":"ARG","Language":"Indian Languages"},{"CountryCode":"ARG","Language":"Italian"},{"CountryCode":"ARG","Language":"Spanish"},{"CountryCode":"ARM","Language":"Armenian"},{"CountryCode":"ARM","Language":"Azerbaijani"},{"CountryCode":"ASM","Language":"English"},{"CountryCode":"ASM","Language":"Samoan"},{"CountryCode":"ASM","Language":"Tongan"},{"CountryCode":"ATG","Language":"Creole English"},{"CountryCode":"ATG","Language":"English"},{"CountryCode":"AUS","Language":"Arabic"},{"CountryCode":"AUS","Language":"Canton Chinese"},{"CountryCode":"AUS","Language":"English"},{"CountryCode":"AUS","Language":"German"},{"CountryCode":"AUS","Language":"Greek"},{"CountryCode":"AUS","Language":"Italian"},{"CountryCode":"AUS","Language":"Serbo-Croatian"},{"CountryCode":"AUS","Language":"Vietnamese"},{"CountryCode":"AUT","Language":"Czech"},{"CountryCode":"AUT","Language":"German"},{"CountryCode":"AUT","Language":"Hungarian"},{"CountryCode":"AUT","Language":"Polish"},{"CountryCode":"AUT","Language":"Romanian"},{"CountryCode":"AUT","Language":"Serbo-Croatian"},{"CountryCode":"AUT","Language":"Slovene"},{"CountryCode":"AUT","Language":"Turkish"},{"CountryCode":"AZE","Language":"Armenian"},{"CountryCode":"AZE","Language":"Azerbaijani"},{"CountryCode":"AZE","Language":"Lezgian"},{"CountryCode":"AZE","Language":"Russian"},{"CountryCode":"BDI","Language":"French"},{"CountryCode":"BDI","Language":"Kirundi"},{"CountryCode":"BDI","Language":"Swahili"},{"CountryCode":"BEL","Language":"Arabic"},{"CountryCode":"BEL","Language":"Dutch"},{"CountryCode":"BEL","Language":"French"},{"CountryCode":"BEL","Language":"German"},{"CountryCode":"BEL","Language":"Italian"},{"CountryCode":"BEL","Language":"Turkish"},{"CountryCode":"BEN","Language":"Adja"},{"CountryCode":"BEN","Language":"Aizo"}]');
alert(countries.length);
I want to get polygon coordinates from below String.
{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}
This is GeoJson string that i decode to array with below code:
$polygon = CJSON::decode($str);
when i want to get polygon i get error!
$var= $polygon->polygon;
or with below code:
$polygon = CJSON::decode($str,true);
$var = $polygon['polygon'];
although for getting coordinates:
foreach($polygon as $key=>$value)
$coordinates = $value['coordinates'];
or
$coordinates = $value[coordinates];
how can i get coordinates from geojson that i send from javascript to php for saving on postgresql with postgis?
$polygon->polygon->geometry->coordinates[0]
or
$polygon['polygon']['geometry']['coordinates'][0]
what you have is a multidimensional array/object not sure which its being output to when decoded in your case as it appears you have a class doing it I would have just used json_decode, but anyway. Yea from the looks of it, polygon is the main object, then in it is geometry which is an object that has type and coordinates, and then coordinates has multiple objects/arrays in it.
the above samples if I typed them right will show the first set of coordinates in that object. Of course you could run it through a loop ie:
In the case that it is an object assuming your Class decodes as an object and not an array. Not exactly sure what $polygon = CJSON::decode($str,true); does. But if its anything like json_decode() then you should be all set.
This is my method of breaking down the object as you present here, its worth noting you may want to check counts, and see if the object is set first, or if the property exists in the object to prevent other means of the code breaking down the road. But what I have here is just pure example at its core, it will server its purpose though. But will not error handle which is why I say you may want to elaborate further on it doing those checks.
Anyway heres my code:
<?php
$str = '{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}';
$polygon = json_decode($str);
echo'<pre>';print_r($polygon);echo'</pre>';
$set = 1;
foreach($polygon->polygon->geometry->coordinates[0] as $coordinates)
{
echo 'Set '.$set.': ';$set++;
echo $coordinates[0].','.$coordinates[1].'<br>';
}
?>
see it in action http://7pz.net/geojson-parse.php (scroll to the bottom)
This should give you an array of all the coordinates and print them out line by line:
$string = '{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}';
$json = json_decode($string);
$coords_array = $json->polygon->geometry->coordinates[0];
foreach($coords_array as $c_a) {
echo $c_a[0] . "," .$c_a[1] . "<br>";
}
Access with:
$coords_array[0];
$coords_array[1];
$coords_array[2];
etc.
Basically you can turn the JSON string into an object and access each element with the -> notation.
I usally use a site called http://jsonviewer.stack.hu/ to decode JSON and find the path I need, then simply write them out as they appear, as in as in the above - $json->polygon->geometry->coordinates;.
Try it out yourself on the site.