如何解码json以获取我的网址中的json数据?

时间:2017-09-08 13:50:37

标签: json getjson

http://example.com/api/orderDetails.php { “USER_ID”:1, “itemList中”:[{ “MAX_PRICE”:120, “MIN_PRICE”:100, “趋势”: “高”, “c_price”:110,“状态“:” 1" , “类型”: “蔬菜”, “ITEM_ID”:19},{ “MAX_PRICE”:200, “MIN_PRICE”:100, “趋势”: “低”, “c_price”:100,“状态“:” 1" , “类型”: “蔬菜”, “ITEM_ID”:224}], “table_no”: “1”}

1 个答案:

答案 0 :(得分:0)

如何从php解码?

 $object_from_json = json_decode('{"user_id":1,"itemList":[{"max_price":120,"min_price":100,"trend":"high","c_price":110,"status":"1","type":"veg","item_id":19},{"max_price":200,"min_price":100,"trend":"low","c_price":100,"status":"1","type":"veg","item_id":224}],"table_no":"1"}');

//or

$array_from_json = json_decode('{"user_id":1,"itemList":[{"max_price":120,"min_price":100,"trend":"high","c_price":110,"status":"1","type":"veg","item_id":19},{"max_price":200,"min_price":100,"trend":"low","c_price":100,"status":"1","type":"veg","item_id":224}],"table_no":"1"}', true);
//then you got a Array in PHP
//var_dump($array_from_json);

//with an URL 
$array_from_json_url = json_decode(file_get_contents($json_url), true);
//var_dump($array_from_json_url);

你需要在你的页面上PHP orderDetails.php添加响应头,需要在echo之前是一个json格式

header("Content-type:application/json");
//$output is array or object PHP
echo json_encode($oupout);