在json响应中访问深层嵌套值的最简单方法是什么?

时间:2015-05-10 17:52:47

标签: php json

我有以下回应(我削减了额外的空头):

{
    "meta": {
        "current_page": "1",
        "last_page": "1",
        "per_page": "15",
        "total": "1",
        "from": "1",
        "to": "1"
      },
      "Products": [
          {
              "archived": "0",
              "committed_stock": "0",
              "created_at": "2015-05-10T17:39:53+00:00",
              "deleted": "0",
              "description": "desc",
              "id": "43061710",
              "links": {
                   "Users": [
                      {
                        "id": "107534",
                        "type": "created_by"
                       }
                    ],
                   "Attributes": [
                     {
                        "id": "31538870"
                     }
                   ]
             }
          }
       ]
}

每当我收到此回复时,“属性”中只会有一个项目。获取此值的最简单方法是什么?到目前为止,我有这个:

  $json = json_decode($json_data);

3 个答案:

答案 0 :(得分:1)

试试这个:

else if

对象字段也可以是以太对象或数组:

  • 请参阅字段:$ object-> object
  • 指数组的i单元格:$ object-> array [i]

P.S。 请编辑json,它错过了它的结束......

答案 1 :(得分:1)

{{1}}

答案 2 :(得分:0)

您可能还想尝试一些PHP的JSON Path库:https://github.com/Peekmo/JsonPath