【问题标题】:How to parse JSON decoded by cURL on PHP如何在 PHP 上解析由 cURL 解码的 JSON
【发布时间】:2016-10-27 08:38:38
【问题描述】:

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

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

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

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

数组 ( [链接] => 数组 ( ) [内容] => 数组 ( [0] => 数组 ( [@type] => ConsumerEnt [链接] => 数组 ([0] => 数组 ( [@type] => 链接 [rel] => GET: 请求 [href] => ………… ) => 数组 ...等

数组中有数组。我找不到如何解析我想要的值。您可以在以下 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
  }
}

【问题讨论】:

标签: php json curl


【解决方案1】:

我想我是这样解决了我的问题的;

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多