Read JSON values from URL via PHP

那年仲夏 提交于 2019-12-08 09:25:26

问题


i have url with this simple JSON {"result":true,"data":[{"Position":"135"}]}

I tried to read it with this:

$json = file_get_contents('https://...link/here..');
$obj = json_decode($json);
echo $obj->result[1]->data->Position;

but it not works. Where i do a mistake? Thanks for helping!


回答1:


You are incorrectly addressing the data in the PHP object created from the JSON String

$json = file_get_contents('https://...link/here..');
$obj = json_decode($json);
echo $obj->data[0]->Position;

Or if there are more occurances

foreach ( $obj->data as $idx=>$data) {
    echo $data->Position;
}


来源:https://stackoverflow.com/questions/41549153/read-json-values-from-url-via-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!