我收到來自谷歌的回覆翻譯V2作爲JSON它看起來像這樣解析谷歌翻譯與PHP的json_decode()V2 JSON
{ "data": { "translations": [ { "translatedText": "Wunderwaffe" } ] } }
然後我把它傳遞給json_decode()
功能,很明顯,我得到一個JSON對象。 但是,我如何獲得translatedText值?
我知道這是一個非常新手的問題,但這就是我與JSON和json_decode()
。
預先感謝您?
我收到來自谷歌的回覆翻譯V2作爲JSON它看起來像這樣解析谷歌翻譯與PHP的json_decode()V2 JSON
{ "data": { "translations": [ { "translatedText": "Wunderwaffe" } ] } }
然後我把它傳遞給json_decode()
功能,很明顯,我得到一個JSON對象。 但是,我如何獲得translatedText值?
我知道這是一個非常新手的問題,但這就是我與JSON和json_decode()
。
預先感謝您?
通過decodedValue->data->translations
或decodedValue['data']['translations']
與foreach()
進行迭代。或者跳過,只需訪問[0]
即可。然後看看translatedText
成員或條目。
$word = addslashes($word);
$url = "http://translate.googleapis.com/translate_a/single?client=p&sl=en&tl=fr&hl=en&dt=bd&dt=md&dt=rm&dt=t&dt=at&dt=sw&q=$word";
// also can use curl
$response = file_get_contents($url);
**$response = preg_replace('/,+/', ',', $response);
$response = preg_replace('/\[,/', '[', $response);**
$responseArray = json_decode($response);
echo "<xmp>";
var_dump($responseArray);
echo "</xmp>";
die;
謝謝!儘管只有在將assoc標誌設置爲json_decode()時纔會起作用,但在我的情況下它並不重要。出於好奇,怎麼可能做同樣的事情而不將json表示爲assoc_array? – paulus
我在我的答案中佔據了兩個位置(儘管我最初使用了錯誤的記號)。 –