将数组作为字符串从"file_get_contents()"转换回数组



当我尝试使用

从API获取数据时
file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");

我得到的结果是一个字符串。有没有办法把它转换回数组?我得到的字符串是

string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}

请帮帮我....

在您的请求中,您要求返回的格式为JSON编码(通过format=json参数),因此您可以使用json_decode将响应解码为数组:

$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);
// Parse the array as you would any other

这里有一个JSON答案,所以我们需要解码它。

<?php
$s =  file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data = 
$data = json_decode($s,true);
?>

和$data变量将是一个数组。

相关内容

  • 没有找到相关文章

最新更新