PHP获取json对象名称

时间:2016-11-23 04:45:16

标签: php

我有像这样的json数组

{
  "STATUS": "SUCCESS",
  "MESSAGE": "",
  "DATA": {
    "list": [
      {
        "val0": {
          "hidden": "0",
          "rate": "10"
        }
      },
      {
        "val1": {
          "hidden": "0",
          "rate": "20"
        }
      }
    ],
    "status": "Provisioned"
  }
}

我想获得DATA>list>的对象名称。 我尝试使用get_class($list[0]),但输出为stdClass。输出应为val0。我该怎么办?请帮忙

3 个答案:

答案 0 :(得分:3)

使用json_decodelist也是一个数组,因此第一个键(0)代表val0,如果你想获得val0的内部内容,那么你必须再次循环它。

$data = '{
  "STATUS": "SUCCESS",
  "MESSAGE": "",
  "DATA": {
    "list": [
      {
        "val0": {
          "hidden": "0",
          "rate": "10"
        }
      },
      {
        "val1": {
          "hidden": "0",
          "rate": "20"
        }
      }
    ],
    "cpe_status": "Provisioned"
  }
}';

$data = json_decode($data, true);

foreach($data['DATA']['list'][0] as $key=>$value){
    echo $key; // return val0
    echo "<br/>";
    // to get val0 inner content
    foreach($value as $item=>$list){
        echo $item." - ".$list; // return val0 inner data
        echo "<br/>";   
    }
}

// alternative way for loop val0 inner content
foreach($data['DATA']['list'][0]['val0'] as $key=>$value){
    echo $key." - ".$value; // return val0 inner data
    echo "<br/>";   
}

答案 1 :(得分:2)

  

希望下面的自我解释片段可以为您提供如何自行完成的提示。顺便说一句,你可以Quick-Test it Here

<?php

        $strJson        = '{
                                      "STATUS"      : "SUCCESS",
                                      "MESSAGE"     : "",
                                      "DATA"        : {
                                                "list"  : [
                                                  {
                                                      "val0"    : {
                                                          "hidden"  : "0",
                                                          "rate"    : "10"
                                                        }
                                                  },
                                                  {
                                                      "val1"    : {
                                                          "hidden"  : "0",
                                                          "rate"    : "20"
                                                       }
                                                }
                                                ],
                                            "cpe_status"  : "Provisioned"
                                      }
                             }';

        $objJson        = json_decode($strJson);
        $data           = $objJson->DATA;
        $lists          = $data->list;
        $list0          = $lists[0];
        $list1          = $lists[1];

        var_dump($lists);
  

以上var_dump($lists)会产生::

        array (size=2)
          0 => 
            object(stdClass)[47]
              public 'val0' => 
                object(stdClass)[46]
                  public 'hidden' => string '0' (length=1)
                  public 'rate' => string '10' (length=2)
          1 => 
            object(stdClass)[49]
              public 'val1' => 
                object(stdClass)[48]
                  public 'hidden' => string '0' (length=1)
                  public 'rate' => string '20' (length=2)
  

现在,您只需使用一个简单的循环就可以从List中获取所需的所有数据:

<?php

        foreach($lists as $objList){
            if(is_object($objList)){
                foreach($objList as $key=>$data){
                    // NOW, YOU CAN DO SOMETHING WITH THE DATA...
                    // LIKE GET THE `hidden` OR `rate` PROPERTIES/ATTRIBUTES
                    $hidden     = $data->hidden;
                    $rate       = $data->rate;
                    // var_dump($hidden);
                    // var_dump($rate);
                    // var_dump($key);
                    var_dump($data);
                }
            }
        }

答案 2 :(得分:1)

$data = '{
                  "STATUS": "SUCCESS",
                  "MESSAGE": "",
                  "DATA": {
                    "list": [
                      {
                        "val0": {
                          "hidden": "0",
                          "rate": "10"
                        }
                      },
                      {
                        "val1": {
                          "hidden": "0",
                          "rate": "20"
                        }
                      }
                    ],
                    "status": "Provisioned"
                  }
                }';
        $data = json_decode($data); 

        $ss = $data->DATA->list;
        echo $ss[0]->val0->rate;