Okay I’m attempting to use php to encode a json file I’m subsequently using to draw a map with the Polymaps service.
I’m having trouble when encoding my json from the Instagram API — I’m able to do it, but I need the structure to be nested in this certain way. My code I have now encodes it in this format:
[
{
"title":"some value",
"src":"some value",
"lat":"some value",
"lon":"some value"
},
...
So on and so forth. I need the format to match something more like this:
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"coordinates":[
-94.34885,
39.35757
],
"type":"Point"
},
"properties":{
"latitude":39.35757,
"title":"Kearney",
"id":919,
"description":"I REALLY need new #converse, lol. I've had these for three years. So #destroyed ! :( Oh well. Can't wait to get a new pair and put my #rainbow laces through. #gay #gaypride #bi #proud #pride #colors #shoes #allstar #supporting ",
"longitude":-94.34885,
"user":"trena1echo5",
"image":"http://images.instagram.com/media/2011/09/09/ddeb9bb508c94f2b8ff848a2d2cd3ece_7.jpg",
"instagram_id":211443415
}
},
{
"type":"Feature",
"geometry":{
"coordinates":[
-0.09402781,
51.51512
],
"type":"Point"
},
"properties":{
"latitude":51.51512,
"title":"City Of London",
"id":918,
"description":"#destroyed",
"longitude":-0.09402781,
"user":"axxoul",
"image":"http://distillery.s3.amazonaws.com/media/2011/09/09/ffda47fb97924a41bc36b0b024fcdfad_7.jpg",
"instagram_id":211074717
}
},
…with its nested structure. I have the jquery working to read from the json file and the php script written to make the json…this is the last puzzle piece for me.
Instead of attempting to manipulate your JSON data with PHP, you could always use PHP’s json_decode() function to convert your JSON into a PHP array.
Once you have your data in an easier format to deal with, you can make all the changes you require to the data using PHP’s array functions and once you are done you can encode it back to a JSON with PHP’s json_encode.