2017-01-05 63 views
1

我正在使用Giscloud的REST API来获取json,并提供一些我需要的信息用于我的web应用程序。问题是,我可以使用相应的php函数从该json获取和/或解码任何数据,这似乎是不可能的。我认为这与json文件的结构有关。无法从json文件中将数据提取到php变量中

这是JSON文件(某些字段编辑):

{ 
    "type": "maps", 
    "total": 3, 
    "page": 1, 
    "data": [ 
    { 
     "id": "edited", 
     "name": "Mapa de Mantención en Linea", 
     "owner": "edited", 
     "epsg": "900913", 
     "active": "1", 
     "copyright": "", 
     "proj4": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m [email protected] +no_defs", 
     "units": "meter", 
     "maxzoom": "0", 
     "mobileacess": "f", 
     "bgcolor": "#417893", 
     "wmsaccess": null, 
     "modified": 1483563460, 
     "accessed": 1483563460, 
     "created": 1483021672, 
     "description": "", 
     "maptype": null, 
     "assets": null, 
     "rating": "0", 
     "units_proj4": "meter", 
     "visited": "31", 
     "resource_id": "6102219", 
     "measure_unit": "meter", 
     "share": "f", 
     "bounds": " {\"xmin\":-8027875.3326789,\"xmax\":-7742459.4690621,\"ymin\":-4065685.5344885,\"ymax\":-3929474.7500843}" 
    }, 
    { 
     "id": "edited", 
     "name": "Mapa de Operaciones", 
     "owner": "edited", 
     "epsg": "900913", 
     "active": "1", 
     "copyright": "", 
     "proj4": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m [email protected] +no_defs", 
     "units": "meter", 
     "maxzoom": "0", 
     "mobileacess": "f", 
     "bgcolor": "#417893", 
     "wmsaccess": null, 
     "modified": 1483563473, 
     "accessed": 1483563473, 
     "created": 1483021672, 
     "description": "", 
     "maptype": null, 
     "assets": null, 
     "rating": "0", 
     "units_proj4": "meter", 
     "visited": "45", 
     "resource_id": "6102603", 
     "measure_unit": "meter", 
     "share": "f", 
     "bounds": "{\"xmin\":-8027263.8364526,\"xmax\":-7741847.9728358,\"ymin\":-4075469.474109,\"ymax\":-3939258.6897048}" 
    }, 
    { 
     "id": "edited", 
     "name": "Mapa M&IC", 
     "owner": "edited", 
     "epsg": "900913", 
     "active": "1", 
     "copyright": "", 
     "proj4": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m [email protected] +no_defs", 
     "units": "meter", 
     "maxzoom": "0", 
     "mobileacess": "f", 
     "bgcolor": "#417893", 
     "wmsaccess": null, 
     "modified": 1483563449, 
     "accessed": 1483563449, 
     "created": 1483021672, 
     "description": "", 
     "maptype": null, 
     "assets": null, 
     "rating": "0", 
     "units_proj4": "meter", 
     "visited": "35", 
     "resource_id": "6102604", 
     "measure_unit": "meter", 
     "share": "f", 
     "bounds": "{\"xmin\":-8028181.080792,\"xmax\":-7742765.2171752,\"ymin\":-4074552.2297696,\"ymax\":-3938341.4453654}" 
    } 
    ] 
} 

这些是我试图解码此JSON,放入一个变量的不同方式。

$json = curl_exec($ch); 

// Number 1 
$data = var_dump(json_decode($json)); 
echo $data['data'][0]['id']; 
exit; 

// Number 1 returns 
object(stdClass)[21] 
    public 'code' => string 'FATAL' (length=5) 
    public 'msg' => string 'Internal error' (length=14) 

// Number 2 
$data = json_decode($json,true); 
echo $data['data'][0]['id']; 

// Number 2 returns 
PHP Undefined index: data 

我试过其他几个类似的函数,比如print_r。或者在这里和这里改变一些值,但没有任何工作。

我将不胜感激这方面的一些帮助!

一些额外的信息:

  1. 有,卷曲没有问题。它正确执行。
  2. 是的,服务器回答一个json文件。

UPDATE

  1. 使用json_last_error()(无需参数)我能debbug。该函数根据错误类型返回一个int值。我的是0,这意味着没有错误。
  2. 我想我已经用尽了这个想法。
+0

JSON字符串验证(使用'jsonlint.com') – RiggsFolly

+0

您的代码返回'编辑'为我,这是正确的。 –

+0

您解码为StdClass,但将该对象用作关联数组... json_decode默认为stdClass:请参阅** [此处的文档](http://php.net/manual/en/function.json-decode .php)** – YvesLeBorg

回答

2

松动var_dump()那只是用于调试,它是从json_decode()返回一个PHP对象stoppping的json_decode()

然后使用正确的对象语法来解决对象

$json = curl_exec($ch); 

$data = json_decode($json); 
// debug code 
if (json_last_error() > 0) { 
    echo json_last_error_msg(); 
} else 
    // this should print the data structure 
    print_r($data); 
} 
// end debug code 

echo $data->data[0]->id; 

如果你想来自所有occurances的id的,你可以做

$json = curl_exec($ch); 

$data = json_decode($json); 
foreach ($data->data as $idx => $obj){ 
    echo "Occurance $idx = " . $obj->id; 
} 
+0

我试过了你的第一个例子。它引发了这个错误: '未定义的属性:stdClass :: $ data' 似乎我认为'json_decode()'在进程中对json中的对象属性做了一些事情,比如修改它们的名字或其他东西,因为当回应我想要的价值似乎不知道该价值存在。 –

+0

好吧我正在检查我没有犯一个愚蠢的错误 – RiggsFolly

+0

顺便说一句:我使用WAMP服务器,并且既然你是一个Wamp用户也不能缺少PHP扩展? –

0

对象语法:

$json = curl_exec($ch); 

$json_data = json_decode($json); 

foreach ($json_data->data as $data_item){ 
    echo '<p>ID: ' . $data_item->id . ' -- Name: ' . $data_item->name . '</p>'; 
    } 

阵列语法:

通过添加第二参数TRUE到json_decode()获得的数据作为一个相关联的阵列php docs

$json = curl_exec($ch); 

$data_array = json_decode($json, true); 

foreach ($data_array['data'] as $item){ 
    echo '<p>ID: ' . $item['id'] . ' -- Name: ' . $item['name'] . '</p>'; 
    } 
0

我不优选使用卷曲在这种情况下,可以使用

file_get_contents 

所以这里是一个简单的代码

$userdata = json_decode(file_get_contents("JSON_URL_GOES_HERE"), true) 
$u_ID = $userdata['data'][0]['id']; 

等 请参阅:file_get_contents

+0

我需要使用cURL。我有一个API密钥。 –

+0

你可以使用你的API密钥完整的网址,并获得相同的数据,这很容易! –