尝试使用PHP格式化json数据以与Polymaps一起使用

时间:2012-04-15 15:58:31

标签: php json

好吧我正在尝试使用php编码一个json文件,我随后用它来绘制Polymaps服务的地图。

我在从Instagram API编码我的json时遇到了麻烦 - 我能够做到,但我需要以这种方式嵌套结构。我的代码现在以这种格式对其进行编码:

[
 {
  "title":"some value",
  "src":"some value",
  "lat":"some value",
  "lon":"some value"
},
...

等等。我需要格式匹配更像这样的东西:

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

...具有嵌套结构。我有jquery工作从json文件读取和编写的PHP脚本来制作json ...这是我的最后一个拼图。

1 个答案:

答案 0 :(得分:0)

您可以使用PHP的json_decode()函数将JSON转换为PHP数组,而不是尝试使用PHP操作JSON数据。

一旦您以更简单的格式处理数据,您就可以使用PHP的array functions对数据进行所需的所有更改,一旦完成,您可以使用PHP {J}将其编码回JSON。 {3}}