使用PHP解码多级JSON

时间:2016-09-20 09:37:37

标签: php json

我有一个多级JSON结果,我将如何在PHP中获取某个值,例如order_id,这里是json结果。

   {
   "metaField":[
      {
         "name":"order_id",
         "autoEntered":1,
         "global":0,
         "maxRepeat":1,
         "resultType":"number"
      },
      {
         "name":"fm_id",
         "autoEntered":0,
         "global":0,
         "maxRepeat":1,
         "resultType":"number"
      },
      {
         "name":"Name",
         "autoEntered":0,
         "global":0,
         "maxRepeat":1,
         "resultType":"text"
      },
      {
         "name":"Surname",
         "autoEntered":0,
         "global":0,
         "maxRepeat":1,
         "resultType":"text"
      },
      {
         "name":"Lever",
         "autoEntered":0,
         "global":0,
         "maxRepeat":1,
         "resultType":"number"
      },
      {
         "name":"othert",
         "autoEntered":0,
         "global":0,
         "maxRepeat":1,
         "resultType":"text"
      }
   ],
   "meta":[
      {
         "recordID":"53",
         "href":"\/RESTfm\/FMServer_Test2\/layout\/resttest\/53.json"
      }
   ],
   "data":[
      {
         "order_id":"100053",
         "fm_id":"53",
         "Name":"Christo",
         "Surname":"Alberts",
         "Lever":"5",
         "othert":"WowWow"
      }
   ],
   "info":{
      "X-RESTfm-Version":"4.0.4\/UNKNOWN",
      "X-RESTfm-Protocol":"5",
      "X-RESTfm-Status":201,
      "X-RESTfm-Reason":"Created",
      "X-RESTfm-Method":"POST",
      "X-RESTfm-Profile":"109ms 2.5M 128M",
      "X-RESTfm-PHP-memory_limit":"128M",
      "X-RESTfm-PHP-post_max_size":"8M"
   }
}

1 个答案:

答案 0 :(得分:0)

解码json

 $json_array = json_decode($json);
 $metaField = $json_array->metaField;
 for($i=0; $i<count($metaField); $i++)
 {
   echo $metaField[$i]->name;
 }