在PHP中解析JSON"stdClass无法转换为字符串".错误 [英] Parsing JSON in PHP "stdClass could not be converted to string" error

查看:89
本文介绍了在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)

这篇关于在PHP中解析JSON"stdClass无法转换为字符串".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆