-4

我调用了一个 url,它以 json 格式返回数据。我喜欢从这个输出中挑选出一些变量。输出如下:

{
  "graph_property" : [ {
    "name" : "calculation_method",
    "value" : "Geo Mean"
  }, {
    "name" : "graph_type",
    "value" : "TIME"
  } ],
  "measurement" : [ {
    "id" : "1132282",
    "alias" : "example.com"
    "bucket_data" : [ {
      "name" : "2013-JUN-20 11:23 AM",
      "id" : 1,
      "perf_data" : {
        "value" : "4.878",
        "unit" : "seconds"
      },
      "avail_data" : {
        "value" : "100.00",
        "unit" : "percent"
      },
      "data_count" : {
        "value" : "1",
        "unit" : "#"
      }
    }, {
      "name" : "2013-JUN-20 11:28 AM",
      "id" : 2,
      "perf_data" : {
        "value" : "-",
        "unit" : "seconds"
      },
      "avail_data" : {
        "value" : "-",
        "unit" : "percent"
      },
      "data_count" : {
        "value" : "-",
        "unit" : "#"
      }
    }, {
      "name" : "2013-JUN-20 11:33 AM",
      "id" : 3,
      "perf_data" : {
        "value" : "-",
        "unit" : "seconds"
      },
      "avail_data" : {
        "value" : "-",
        "unit" : "percent"
      },
      "data_count" : {
        "value" : "-",
        "unit" : "#"
      }
    } ],
    "graph_option" : [ {
      "name" : "perfwarning",
      "value" : "-",
      "unit" : "seconds"
    }, {
      "name" : "perfcritical",
      "value" : "-",
      "unit" : "seconds"
    }, {
      "name" : "availwarning",
      "value" : "-",
      "unit" : "percent"
    }, {
      "name" : "availcritical",
      "value" : "-",
      "unit" : "percent"
    }, {
      "name" : "bucketsize",
      "value" : "300",
      "unit" : "seconds"
    }, {
      "name" : "rows",
      "value" : "3",
      "unit" : "#"
    }, {
      "name" : "pagecomponent",
      "value" : "User Time",
      "unit" : "seconds"
    }, {
      "name" : "avg_perf",
      "value" : "4.878",
      "unit" : "seconds"
    }, {
      "name" : "avg_avail",
      "value" : "100.00",
      "unit" : "percent"
    }, {
      "name" : "total_datapoint_count",
      "value" : "1",
      "unit" : "#"
    }, {
    } ]
  } ],
  "link" : {
    "type" : "application/json",
    "href" : "http://api.keynote.com/",
    "rel" : "slotmetadata"
  }
}

我感兴趣的价值观是:

 "

name" : "2013-JUN-20 11:23 AM",
     "value" : "4.878",

     "name" : "2013-JUN-20 11:28 AM",
     "value" : "-",

    "name" : "2013-JUN-20 11:33 AM",
     "value" : "-",

我这样做的原因是,有时 Web 服务调用会返回 emtpy 值。从这个输出中,我喜欢选择没有空值的最新日期,然后打印出不为空的名称和值。

有没有一种简单的方法可以从此 json 输出中选择名称和值字段?

当我做

$data<-json_decode($resp)
print_r($data)

数据的一部分是这样的:

[measurement] => Array
    (
        [0] => Array
            (
                [id] => 1132282
                [alias] =>example.com
                [bucket_data] => Array
                    (
                        [0] => Array
                            (
                                [name] => 2013-JUN-20 01:23 PM
                                [id] => 1
                                [perf_data] => Array
                                    (
                                        [value] => 3.074
                                        [unit] => seconds
                                    )

                                [avail_data] => Array
                                    (
                                        [value] => 100.00
                                        [unit] => percent
                                    )

                                [data_count] => Array
                                    (
                                        [value] => 1
                                        [unit] => #
                                    )

                            )

                        [1] => Array
                            (
                                [name] => 2013-JUN-20 01:28 PM
                                [id] => 2
                                [perf_data] => Array
                                    (
                                        [value] => 3.416
                                        [unit] => seconds
                                    )

                                [avail_data] => Array
                                    (
                                        [value] => 100.00
                                        [unit] => percent
                                    )

                                [data_count] => Array
                                    (
                                        [value] => 1
                                        [unit] => #
                                    )

                            )

                        [2] => Array
                            (
                                [name] => 2013-JUN-20 01:33 PM
                                [id] => 3
                                [perf_data] => Array
                                    (
                                        [value] => -
                                        [unit] => seconds
                                    )

                                [avail_data] => Array
                                    (
                                        [value] => -
                                        [unit] => percent
                                    )

                                [data_count] => Array
                                    (
                                        [value] => -
                                        [unit] => #
                                    )

                            )

                    )
4

1 回答 1

1

使用内置的json_decode函数

$data = json_decode('your_json_here'); //$data will be "stdClass" object
$data = json_decode('your_json_here', true); //$data will be "array" object

然后您可以像使用常规数组/对象一样使用 $data 进行操作

如果类型 = 标准类:

$data->measurement[0]->bucket_data[0]->name; // will be equal "2013-JUN-20 11:23 AM"

如果类型 = 数组

$data['measurement'][0]['bucket_data'][0]['name']; // will be equal "2013-JUN-20 11:23 AM"
于 2013-06-20T15:49:45.917 回答