实际上这是一个字符串,
如何将其转换为对象或数组..
{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}
我试过这个
$array=explode(' ',$result);
$json = json_encode($result);
print($json);
它给我这样的结果
"{\"kind\": \"delivery_quote\", \"fee\": 750, \"created\": \"2016-02-28T19:13:38Z\", \"expires\": \"2016-02-28T19:18:38Z\", \"currency\": \"usd\", \"duration\": 60, \"dropoff_eta\": \"2016-02-28T20:18:38Z\", \"id\": \"dqt_KhC5sbjq00Jn6F\"}"
但我怎样才能正确地做到这一点,以便我可以像
那样取得结果echo $json->fee;
这是迄今为止我所拥有的Eval。
帮助
答案 0 :(得分:2)
您正在尝试对json进行编码。 json_encode返回值的JSON表示形式。如果要将json转换为数组,则应使用json_decode作为:
$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}';
$json = json_decode($result);
echo $json->fee;
将费用的值作为输出:
750
答案 1 :(得分:1)
<?php
$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}';
var_dump(json_decode($result));
var_dump(json_decode($result, true));