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

vin*_*per 1 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>
Run Code Online (Sandbox Code Playgroud)

这就是它打印的内容.

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
Run Code Online (Sandbox Code Playgroud)

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

我试过这个

json_decode($response,true));
Run Code Online (Sandbox Code Playgroud)

还有这个

ksort($response);
Run Code Online (Sandbox Code Playgroud)

还有这个

var_dump($response);
Run Code Online (Sandbox Code Playgroud)

什么都没有效果..

我希望能够像这样输出

echo $reponse['Name'];
Run Code Online (Sandbox Code Playgroud)

有帮助吗?谢谢

mst*_*rdy 10

json_decode您将"true"作为第二个参数传递时,为您提供一个关联数组:

  $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"];
Run Code Online (Sandbox Code Playgroud)

得到:

Test
Run Code Online (Sandbox Code Playgroud)

编辑:

json_decode()给你一个数组数组,所以你需要引用[0]响应中位置的数组,如果你得到我的话.

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

  $result = json_decode($json, true);
  var_dump($result);
Run Code Online (Sandbox Code Playgroud)

得到:

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)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后..访问数组本身:

  $result = json_decode($json, true);

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

得到:

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  echo "Name: ". $result["Name"] . "\nID:   " . $result["Id"] . "\nDescription: " . $result["Description"] . "\n";
Run Code Online (Sandbox Code Playgroud)

现在我们回来了:

Name: Test 
ID:   1079
Description: This is a Test Event
Run Code Online (Sandbox Code Playgroud)

希望有道理!


小智 6

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

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)
Run Code Online (Sandbox Code Playgroud)