是否可以从laravel中的(key-value)对json对象打印Key的值

时间:2018-04-23 10:56:26

标签: json laravel laravel-5 laravel-5.2 laravel-5.1

如何从laravel中的json对象打印'key'值。

json数组:

 "pricing": {
"in": {
  "categories": [
    "ccTLD",
    "Geography"
  ],
  "addons": {
    "dns": true,
    "email": true,
    "idprotect": true
  },
  "group": "",
  "register": {
    "1": "704.39"
  },
  "transfer": {
    "1": "704.39"
  },
  "renew": {
    "1": "704.39"
  }
},

如何从上述数据中显示“in”?

2 个答案:

答案 0 :(得分:0)

JSON放入变量中。防爆。 $jsonVariable

json_decode($jsonVariable)->pricing->in

答案 1 :(得分:0)

首先,我认为你的json中存在一些错误,这是我用两种方式实现相同的功能

public function demo()
    {
        $json_array = '
        {
                "pricing": {

                        "in": {
                        "categories": [
                                "ccTLD",
                                "Geography"
                            ],
                        "addons": {
                                "dns": true,
                                "email": true,
                                "idprotect": true
                            },
                            "group": "",
                            "register": {
                                "1": "704.39"
                            },
                            "transfer": {
                                "1": "704.39"
                            },
                            "renew": {
                                "1": "704.39"
                            }
                        }

                    }
             }
         ';
        echo "<pre>";
       print_r(json_decode($json_array)->pricing->in);
       $asso_array = json_decode($json_array, true);
        print_r($asso_array["pricing"]["in"]);
       exit;
    }