简体   繁体   English

用php解析google translation v2 json json_decode()

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

I receive reply from Google Translate v2 as json which looks like this 我收到了来自Google Translate v2的json回复,如下所示

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

then I pass it to json_decode() function and obviously I get a JSON object. 然后我将其传递给json_decode()函数,显然我得到了一个JSON对象。 But how do I get translatedText value? 但是,如何获得TranslationText值?

I know it's a really newbie question, but that's what I am with JSON and json_decode() . 我知道这是一个真正的新手问题,但这就是JSON和json_decode()

Thank you in advance? 先感谢您?

Iterate over decodedValue->data->translations or decodedValue['data']['translations'] with foreach() . 使用foreach() decodedValue->data->translationsdecodedValue['data']['translations'] Or take the leap and just access [0] of it. 或跳过并仅访问它的[0] And then look at the translatedText member or entry. 然后查看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