-1

我很难将 DistanceMatrix API 结果转换为 JSON 格式。

这是$response(我在 Laravel 5.1 中使用 Ivory\googlempas 插件)的 var_dump

object(Ivory\GoogleMap\Services\DistanceMatrix\DistanceMatrixResponse)#694 (4) { ["status":protected]=> string(2) "OK" ["destinations":protected]=> array(1) { [0]=> string(12) "Milan, Italy" } ["origins":protected]=> array(1) { [0]=> string(11) "Rome, Italy" } ["rows":protected]=> array(1) { [0]=> object(Ivory\GoogleMap\Services\DistanceMatrix\DistanceMatrixResponseRow)#693 (1) { ["elements":protected]=> array(1) { [0]=> object(Ivory\GoogleMap\Services\DistanceMatrix\DistanceMatrixResponseElement)#692 (3) { ["status":protected]=> string(2) "OK" ["distance":protected]=> object(Ivory\GoogleMap\Services\Base\Distance)#690 (2) { ["text":protected]=> string(6) "572 km" ["value":protected]=> int(572343) } ["duration":protected]=> object(Ivory\GoogleMap\Services\Base\Duration)#691 (2) { ["text":protected]=> string(15) "5 hours 42 mins" ["value":protected]=> int(20517) } } } } } } 

我如何在 JSON 数组中返回它?

4

1 回答 1

0

尝试使用类型提示:

$json = json_encode( (array)$response );

更新:

这有点hacky,但会解决您的问题。
考虑到您已将对象编码为$json变量中的 json,您可以使用str_replace()方法替换不需要的/u0000即空字符。

$json = str_replace('\\u0000', '',$json);
于 2016-08-12T19:08:58.867 回答