1

I have following JSON Data and I tried to display Json data using PHP but it did not display result. Here is my JSON Data

$ads={"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}

I tried following code but it did not work.

$ads1 = json_decode($ads);

echo $ads1->ReferenceNo;

Kindly help me how to display result in PHP. Thanks in advance.

3
  • var_dump($ads1);, it could be an array. Commented Mar 13, 2015 at 8:54
  • 2
    This is not a proper json. Commented Mar 13, 2015 at 8:54
  • if you have proper json use $data = json_decode($ads,true); and than $data['ReferenceNo']; Commented Mar 13, 2015 at 8:57

2 Answers 2

6

You should parse data as string & then do like this:

$json = '{"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}';
$array = json_decode($json, true);
echo '<pre>'; print_r($array);

Output:

Array
(
    [ReferenceNo] => KWPOG0QoXU
    [Message] => SUCCESS
    [Status] => 1
    [ResponseStatus] => 200
)

To get Data, Code like this:

echo $array['ReferenceNo'];

Let me know for further help.

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

Comments

1

For people interested in pulling data from a location, instead of adding the json on the file, use the following;

   $json = file_get_contents('MYLOCATION.json');
   $array = json_decode($json, true);
   echo '<pre>'; print_r($array);

happy coding :)

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.