如何在PHP上解析由cURL解码的JSON

时间:2016-10-27 08:38:39

标签: php json curl

我正在尝试使用REST API制作一些带有php和curl的页面。我可以连接应用程序并使用curl在PHP上获取所有值为JSON格式的值。但是,我无法解析我想要的价值观。

我使用这个PHP命令来解码json值;

$data = json_decode($response, true);
print_r($data);

当我使用这个命令时,我会得到这样的返回值;

  

数组([links] =>数组()[内容] =>数组([0] =>数组(   [@type] => ConsumerEnt [links] =>数组([0] =>   数组([@type] => link [rel] => GET:请求[href] =>   HTTP .....   )=>数组...等

数组中有数组..我找不到如何解析我想要的值。您可以在以下json代码中看到我想要的值;

你知道怎么可能吗?

You can see Decoded JSON by http://json.parser.online.fr/

{
  "links": [],
  "content": [
    {
      "@type": "ConsumerEnt",
      "links": [
        {
          "@type": "link",
          "rel": "GET: Request",
          "href": "https://ip/url"
        },
        {
          "@type": "link",
          "rel": "POST:Request",
          "href": "https://ip/url"
        }
      ],
      "entitledOrg": [
        {
          "tRef": "local",
          "tLabel": "local",
          "sRef": "768882f2-cf89-4cc8-92b7",
          "sLabel": "USsB01"
        }
      ],
      "citems": "a0221dfd-00f9-4019-95ed",
      "name": "NAME I WANT TO GET",
      "description": "Basic",
      "isNoteworthy": false,
      "dateCreated": "2016-09-25T11:44:02.698Z",
      "lastUpdatedDate": "2016-09-25T12:46:45.474Z",
      "iconId": "a0221dfd-00f9-4019-95ed-a74faeb2e1b2",
      "catalogItemTypeRef": {
        "id": "com.company",
        "label": "Compo"
      },
      "serviceRef": {
        "id": "7c92b9a5-9dd5-4819-9320-4127b1e3009d",
        "label": "NAME I WANT TO GET"
      },
      "outputResourceRef": {
        "id": "composition.type",
        "label": "test"
      }
    },
    {
      "@type": "ConsumerEnti",
      "links": [
        {
          "@type": "link",
          "rel": "GET: Request",
          "href": "https://ip/url"
        },
        {
          "@type": "link",
          "rel": "POST:Request",
          "href": "https://ip/url"
        }
      ],
      "entitledOrg": [
        {
          "tRef": "local",
          "tLabel": "local",
          "sRef": "768882f2-cf89-4cc8-92b7",
          "sLabel": "astDF1"
        }
      ],
      "catalogItemId": "89b43bcb-4b23-4fc0-af26-66bd5c6bd1bc",
      "name": "NAME I WANT TO GET",
      "description": "RASDhFASel",
      "isNoteworthy": false,
      "dateCreated": "2016-10-27T07:55:03.243Z",
      "lastUpdatedDate": "2016-10-27T07:56:00.754Z",
      "iconId": "compos.png",
      "catalogItemTypeRef": {
        "id": "com.company",
        "label": "Compo"
      },
      "serviceRef": {
        "id": "7c92b9a5-9dd5-4819-9320-4127b1e3009d",
        "label": "NAME I WANT TO GET"
      },
      "outputResourceRef": {
        "id": "composition",
        "label": "Dep"
      }
    }
  ],
  "metadata": {
    "size": 20,
    "totalElements": 2,
    "totalPages": 1,
    "number": 1,
    "offset": 0
  }
}

1 个答案:

答案 0 :(得分:0)

我想我解决了这个问题;

$data = json_decode($response, true);
print_r($data);

  $j=count($data["content"]);

                for ( $say=0 ; $say < $j ; $say++ )
                {

                    print $data["content"][$say]["name"];

                }

因此我可以列出我的产品;

产品1
产品2
产品3