2017-02-21 170 views
1

我需要从api获取特定数据的帮助。 JSON数据看起来像这样如何从json获取特定数据?

array(1) 
{ 
[0]=> string(18) "{"errorCode":0 

    "members":[ 

    {"userId":"32" 
    "name":"Joy" 
    "age":"22" 
    "country":"USA" 
    "orders":133 
    "active":0} 

    {"userId":"38" 
    "name":"greg" 
    "age":"29" 
    "country":"CA" 
    "orders":19 
    "active":0} 

    {"userId":"29" 
    "name":"bob" 
    "age":"33" 
    "country":"USA" 
    "orders":67 
    "active":0} 


],"total":59}, 
Array" 
    } 

我想回去只有人在美国是超过25

到目前为止我的代码是

<?PHP 

$url="https://api.someplace.com"; 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $url); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
$content = curl_exec($ch); 
$response = curl_getinfo($ch); 
$combine = "$content, $response"; 
curl_close ($ch); 

$openBracket = str_replace("{","{\n",$combine); 
$comma = str_replace(",",",\n",$openBracket); 
$result = str_replace(',', '<br />', $comma); 

$ary = array($result); 
echo $ary; 

?> 

这将返回的是整个json文件,但是当我试图对数据做任何事情的时候都不起作用。我知道我需要某种foreach循环,if/else语句或者json_decode,我不会因为它们没有工作而让我感到厌烦,因为它们都不起作用,我只返回所有json数据的返回,NULL,{ ,或只是一个空白页面。

我不确定cURL是否搞乱了事情,我以前从来没有使用过,因为这是https连接,所以我不得不使用它。请让我知道获取这些数据的最佳方式。

+0

如果你想获得'$ content'和'$ response'把它们放在一起作为阵列'$ =结合阵列()第一件事; $ combine ['content'] = $ contente; $ combine ['info'] = $ info;'你可以根据需要循环条件。 –

+0

[用PHP解析JSON文件]可能的副本(http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – miken32

+0

可能的重复[如何从JSON中用PHP提取数据?] (http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) –

回答

0

首先,提供的内容包含无效的JSON数据(正如您已经可以自己说明的那样)。所以你将不得不解析它。

如果您需要帮助解析让我知道。它可以很容易地用正则表达式来完成。

将内容转换为有效的JSON后,可以使用json_decode函数将其转换为数组,以便于访问。
用法:

$data = $valid_json; 
$array = json_decode($data, true); 

true参数将结果转换成一个阵列,而不是一个对象(stdClass的)。
实例(编译数组JSON代码时):

$json_data = json_encode([ 
    'apple_amount' => 14 
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); 

你并不需要使用JSON_PRETTY_PRINT。它用于返回“漂亮”的结果。 JSON_UNESCAPED_UNICODE用于保存unicode字符。
实例(编译JSON到一个数组):

$result = json_decode($json_data, true); 
$apples = $result['apple_amount']; 

我几乎可以肯定你的问题是从无效的JSON起源,我不知道你想达到什么在这里:

$openBracket = str_replace("{","{\n",$combine); 
$comma = str_replace(",",",\n",$openBracket); 
$result = str_replace(',', PHP_EOL, $the); 
1

其一,你错过了,当你调用curl_exec而不是将其输出到标准输出是告诉curl 回报内容的所有重要卷曲选项CURLOPT_RETURNTRANSFER

其次,你正在做一些奇怪的操作,完全打破JSON。

这应该是足够了:

<?php 

$url = "https://api.someplace.com"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $url); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$content = curl_exec($ch); 
$response = curl_getinfo($ch); 

// $combine = "$content, $response"; // this is useless 
curl_close ($ch); 

$response = json_decode($content, true); 

var_dump($response); // decoded JSON, accessible as an array 
+0

谢谢,这让它工作。 – nkr