簡體   English   中英

用php解析google translation v2 json json_decode()

[英]parse google translate v2 json with php json_decode()

我收到了來自Google Translate v2的json回復,如下所示

{ "data": { "translations": [ { "translatedText": "Wunderwaffe" } ] } }

然后我將其傳遞給json_decode()函數,顯然我得到了一個JSON對象。 但是,如何獲得TranslationText值?

我知道這是一個真正的新手問題,但這就是JSON和json_decode()

先感謝您?

使用foreach() decodedValue->data->translationsdecodedValue['data']['translations'] 或跳過並僅訪問它的[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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM