2011-04-03 49 views
0

我有這個字符串:解析JSON在PHP「stdClass的不能轉換爲字符串」錯誤

{\"lat\":37.790388261934424,\"lng\":-122.46047996826172},{\"lat\":37.789608231530124,\"lng\":-122.46344112701416} 

然後感謝StackOverflowers的幫助下,我這樣做是爲了剝奪斜線:

$markers = stripslashes($markers); 

然後我嘗試添加這似乎對由解碼功能

$markers = json_decode('['.$markers.']'); 

是需要外部支架正確的方式去呢?然後我試圖做到這一點:

foreach($markers as $key => $value) 
{ 
    $some_string = $some_string.' ('.$value.') '; 
} 

哪款遊戲我這個錯誤:

Object of class stdClass could not be converted to string 

但我真正需要的是緯度/經度值提取到$緯度,$ LNG變量。有人可以告訴我如何解決我得到的錯誤,並將值放入變量中?

謝謝, 亞歷克斯

回答

5

嘗試...

foreach($markers as $marker) 
{ 
    $some_string .= '('.$marker->lat.','.$marker->lng.')'; 
} 

結果...

(37.7903882619,-122.460479968)(37.7896082315,-122.463441127) 
3
foreach($markers as $key => $value) 
{ 
    // $value is object with lat and lng properties 
    $some_string = $some_string.' (lat:'.$value->lat.') '; 
} 
0

這是一些困惑我也一樣,爲什麼json_decode()返回封裝在對象中的數據?

事實證明,Javascript中的所有數組都被視爲對象,因此json_decode()將它們作爲PHP stdClass的對象返回。嘗試在$標記上執行print_r()以查看它的結構。

0

這這裏工作

var json = JSON.parse(<?php echo "'" . stripslashes($_POST['json']) . "'" ?>); 
+0

這真的沒有解決的問題比任何其他的答案做的更好。 – 2012-09-23 20:05:10

+0

什麼是php json解析的javascript代碼? – endyourif 2012-09-24 01:57:11

相關問題