douye5949 2017-09-15 16:06
浏览 34
已采纳

使用php修改JSON Feed

I have a JSON Feed which is accessed by an api. The json feed it returns is as below:

[  
   {  
      "isoDate":"2017-09-15T00:00:00.0000000",
      "events":[  
         {  
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   },
   {  
      "isoDate":"2017-09-16T00:00:00.0000000",
      "events":[  
         {  
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   }
]

I need the isoDate to be listed with each event instead of individually. e.g.

[  
   {  
      "events":[  
         {  
            "isoDate":"2017-09-15T00:00:00.0000000",
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "isoDate":"2017-09-15T00:00:00.0000000",
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   },
   {  
      "events":[  
         {  
            "isoDate":"2017-09-16T00:00:00.0000000",
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "isoDate":"2017-09-16T00:00:00.0000000",
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   }
]

Can this be achieved with php? Basically fetch that feed from a url and then display it in my preferred format?

  • 写回答

3条回答 默认 最新

  • dongxinxin7809 2017-09-16 08:57
    关注

    So this is what you have to do, to get back your desired format of the json, $json is your json string:

    $eventList = json_decode($json);
    
    foreach($eventList as $eventEntry){
        $isoDate = $eventEntry->isoDate;
    
        foreach($eventEntry->events as $subEventEntry){
            $subEventEntry->isoDate = $isoDate;
        }
    
        //delete the isoDate from outer
        unset($eventEntry->isoDate);
    }
    
    echo json_encode($eventList);
    

    So basically, you are first decoding your json into php structure, apply your changes and after that, encode it back. Note here, that I have not appened true as second parameter for the $json_decode, but working with the resulting object.

    Also: Your json is not standard comform and could result in errors. PHP will properly not decode it, because your object end with a comma. The last element of an object should be without comma. Instead of

    {  
        "id":"-91417",
        "name":"Torget - a multi cultural meeting place in Geilo",
    }
    

    make it like this:

    {  
        "id":"-91417",
        "name":"Torget - a multi cultural meeting place in Geilo"
    }
    

    I know, this can be a problem, when you get it from an API, but this is another problem of itself...

    EDIT:

    To get every "events" into one big array, you have to store them just like your imagination ;) . Think it like this: $subEventEntry holds one "events"-object. Because you are iterating both levels, you see everyone object of them. My suggestion would be to store them in a new array, and recreating the structure around it:

    $everything = new stdClass();
    $everything->events = array();
    

    and then, in the inner loop:

    foreach($eventList as $eventEntry){
        $isoDate = $eventEntry->isoDate;
    
        foreach($eventEntry->events as $subEventEntry){
            $subEventEntry->isoDate = $isoDate;
            $everything->events[] = $subEventEntry; // <-- this has to be added
        }
    
        //delete the isoDate from outer
        unset($eventEntry->isoDate);
    }
    

    When recreating the structure, and you don't need the old structure anymore you could remove the unset.

    Just remeber every [ ] pair in the json represents an array, every { } pair an object (stdClass). The name of this object/array is referenced -> by its class property in the superobject.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算