在PHP中解析JSON"stdClass无法转换为字符串".错误
问题描述:
我有这个字符串:
{\"lat\":37.790388261934424,\"lng\":-122.46047996826172},{\"lat\":37.789608231530124,\"lng\":-122.46344112701416}
然后,在StackOverflowers的帮助下,我这样做是为了去除斜线:
Then thanks to the help of StackOverflowers, I did this to strip the slashes:
$markers = stripslashes($markers);
然后我尝试添加解码功能似乎需要的外括号
Then I tried to add the outside brackets which seemed to be needed by the decode function
$markers = json_decode('['.$markers.']');
这是正确的做法吗?然后我尝试这样做:
Was that the right way to go about it? Then I tried to do this:
foreach($markers as $key => $value)
{
$some_string = $some_string.' ( '.$value.' ) ';
}
游戏我这个错误:
Object of class stdClass could not be converted to string
但是我真正需要的是将lat/lng值提取到$ lat,$ lng变量中.有人可以建议我如何解决我得到的错误,然后将值放入变量中?
But what I really need is to extract the lat/lng values into $lat , $lng variables. Could someone suggest to me how to fix the error I am getting and put the values into the variables?
谢谢, 亚历克斯
答
尝试...
foreach($markers as $marker)
{
$some_string .= '('.$marker->lat.','.$marker->lng.')';
}
结果...
(37.7903882619,-122.460479968)(37.7896082315,-122.463441127)