0

Possible Duplicate:
How to parse a JSON string using PHP

this is my data object

{
  "data": {
    "translations": [
      {
        "translatedText": "Hallo Welt"
      },
      {
        "translatedText": "Hallo Berlin"
      }
    ]
  }
}

how do I parse this using PHP?

this is a jsonObject that contains jsonObject("data") that contains jsonArray that contains jsonObjects at each index that contains key/value "translatedText"

this is what I have and my assumption

$jsonResult = json_decode($data);
$translated_text = $jsonResult->data->translations[0]->translatedText;`
3
  • 1
    Have you tried: json_decode()? Commented Nov 26, 2012 at 19:16
  • Have you tried: var_dump($jsonResult);? Commented Nov 26, 2012 at 19:19
  • 1
    Actually, this is a fair question. If you come to PHP from other languages PHP handles JSON differently. There are no jsonArray or jsonObject types in PHP (like in Java or C#). Commented Nov 26, 2012 at 19:28

3 Answers 3

1
$array = json_decode($json_element, true);

to make associative array.

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

Comments

1

I'm think this is what it would be. json_decode does not parse to a PHP object, but to just an array.

$jsonResult = json_decode($data);
$translated_text = $jsonResult['data']['translations'][0]['translatedText'];

Comments

0
$array = json_decode($json_element);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.