0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! although that would only work if I set assoc flag to json_decode(), but in my case it doesn't really matter. Just out of curiosity, how is it possible to do the same thing without representing json as assoc_array?
I accounted for both in my answer (I used incorrect notation initially though).
0
$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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.