将cURL返回的JSON数组转换为关联数组

4

我有一个类似这样的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'];

有需要帮忙的吗?谢谢。

你提到 var_dump($response); “不起作用” - 它给出了什么? 你是在努力从CURL调用中获取JSON字符串,还是在努力将其解码为数组? - IMSoP
如果我执行以下代码 $response = var_dump($response); echo $response['Name'];,输出结果为 bool(true)。看起来从 CURL 中获取 JSON 字符串没问题,只是无法将其转换为关联数组。 - vinylDeveloper
@IMSoP 我认为这与我的返回字符串有关。一些值是整数和数组,它们没有引号。你知道我该怎么处理吗?那可能是问题所在吗? - vinylDeveloper
json_decode($response, true)); 对我有用。 - vishalknishad
4个回答

16

json_decode函数在第二个参数传入"true"时会返回一个关联数组:

json_decode gives you an associative array when you 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

希望这有意义!


6

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

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)

3

我有一个类似于这样的cURL结果。

{
  "object": "transaction",
  "status": "paid",
}

所以我这样做了。

$result = json_decode($response, true);
$result['status'];

结果将是:
paid

0
我发现的方法是删除响应的所有头部,找到 JSON 响应中第一个字符的位置,在你的情况下是 '[' 但也可能是 '{',然后执行以下操作:
$json = '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]}] ';
$pos = strpos($json,'[');
$json = substr($json,$pos);
$php_array = json_decode($json, true);
echo "Name= ".$php_array[0]['Name']; //because it starts with [ you need to add [0]

你会得到 名称= 测试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接