简体   繁体   中英

parse google translate v2 json with php json_decode()

I receive reply from Google Translate v2 as json which looks like this

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

then I pass it to json_decode() function and obviously I get a JSON object. But how do I get translatedText value?

I know it's a really newbie question, but that's what I am with JSON and json_decode() .

Thank you in advance?

Iterate over decodedValue->data->translations or decodedValue['data']['translations'] with foreach() . Or take the leap and just access [0] of it. And then look at the translatedText member or entry.

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM