paulus
paulus

Reputation: 655

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?

Upvotes: 0

Views: 1405

Answers (2)

Morteza Sepehri Niya
Morteza Sepehri Niya

Reputation: 414

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

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

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.

Upvotes: 1

Related Questions