将cURL json数组响应转换为关联数组

时间:2013-07-01 01:30:26

标签: php json curl

我有这样的cURL请求

$ch = curl_init();
$data = 'filter=year(StartTime)' . urlencode(' eq 2013 and ') .'month(StartTime)'. urlencode(' eq 06') ;
curl_setopt($ch, CURLOPT_URL, "http://url.com/id()/events?$".$data);
$headers = array(
    'Accept: application/json',
    'Content-type: application/json',
    'X-ApiKey : XXXXXXXXXX'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);

$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);

?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>


</head>

<body>

<br><br>
<?php 
 echo "the name". $result['Name'];
?>

</body>
</html>

这是它打印的内容。

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}] 

the name

如何将其置于关联数组中?

我试过这个

json_decode($response,true));

和这个

ksort($response);

和这个

var_dump($response);

似乎没有任何作用..

我希望能够像这样输出

echo $reponse['Name'];

有任何帮助吗?感谢

2 个答案:

答案 0 :(得分:10)

json_decodeyou pass "true" as the second argument

时为您提供关联数组
  $json = '[{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}]';

  $response = json_decode($json, true);

  echo $response[0]["Name"];

给出:

Test

修改

json_decode()会返回一个数组数组,因此如果你找到我,你需要在响应中引用位于[0]的数组。

我已经在我的例子中用$ response [0]完成了这个,但看看这个例子,希望它能让它更清晰!

  $result = json_decode($json, true);
  var_dump($result);

给出:

array(1) {
  [0]=>
  array(10) {
    ["Id"]=>
    int(1079)
    ["Name"]=>
    string(5) "Test "
    ["Status"]=>
    int(1)
    ["MemberId"]=>
    int(1308)
    ["Description"]=>
    string(20) "This is a Test Event"
    ["SponsoredBy"]=>
    NULL
    ["StartTime"]=>
    string(19) "2013-06-30T12:00:00"
    ["EndTime"]=>
    string(19) "2013-06-30T23:59:00"
    ["SearchDescription"]=>
    NULL
    ["Types"]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(4)
      [2]=>
      int(6)
    }
  }
}

然后..访问数组本身:

  $result = json_decode($json, true);

  $result = $result[0]; // let's just reassign this to get the array we want      
  var_dump($result);

给出:

array(10) {
  ["Id"]=>
  int(1079)
  ["Name"]=>
  string(5) "Test "
  ["Status"]=>
  int(1)
  ["MemberId"]=>
  int(1308)
  ["Description"]=>
  string(20) "This is a Test Event"
  ["SponsoredBy"]=>
  NULL
  ["StartTime"]=>
  string(19) "2013-06-30T12:00:00"
  ["EndTime"]=>
  string(19) "2013-06-30T23:59:00"
  ["SearchDescription"]=>
  NULL
  ["Types"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(4)
    [2]=>
    int(6)
  }
}

现在您可以直接访问数组的各种元素:

  $result = json_decode($json, true);
  $result = $result[0];

  echo "Name: ". $result["Name"] . "\nID:   " . $result["Id"] . "\nDescription: " . $result["Description"] . "\n";

现在我们回来了:

Name: Test 
ID:   1079
Description: This is a Test Event

希望有意义!

答案 1 :(得分:3)

默认情况下,curl_exec将从服务器获取的数据输出到标准输出,因此$ response变量实际上没有实际的响应数据。如果要在变量中获取数据,请在调用curl_exec之前设置CURLOPT_RETURNTRANSFER选项。

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)