繁体   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