字符串到数组或对象
String to array or object
其实这是一个字符串,
如何将其转换为对象或数组..
{"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。
求助
<?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));
您正在尝试对 json 进行编码。 json_encode Returns the JSON representation of a value. If you want to convert json to array then you should use 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
其实这是一个字符串,
如何将其转换为对象或数组..
{"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。
求助
<?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));
您正在尝试对 json 进行编码。 json_encode Returns the JSON representation of a value. If you want to convert json to array then you should use 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