0

I used to decode a json string stored in a variable. Now i've come across a situation where a php file is passed a set of json data.

I'm not aware of how to get the data and then decode it to php array.

please do not give answers like

$variable = '{ "data1" : "value1" }';

I've used this before.

//Edit

Also I'm not asking how to encode a php array to json.

Thanks.

Ashik Basheer
  • 1,531
  • 3
  • 16
  • 36
  • Mmmh. https://encrypted.google.com/search?q=php+decode+json `->` http://no1.php.net/json_decode. But you also mention that you don't know how to *get* the data. Well, how is the Android app trying to transfer the data to the PHP script? Without more context, we cannot really help you. – Felix Kling Dec 05 '13 at 07:26
  • Hi Felix, the data coming from android is simple { "username": "value1", "password" : "value2" } I need to catch this data in PHP and then decode them to process further. – Ashik Basheer Dec 05 '13 at 07:37
  • Ok, so you know how to get the data. As I showed, googling for `php decode json` would have given you the answer you needed. Didn't you search before? – Felix Kling Dec 05 '13 at 07:39
  • @Felix thank you so much. I got the answer here http://stackoverflow.com/questions/14914628/php-decode-json-post – Ashik Basheer Dec 05 '13 at 07:53
  • 1
    So, the information you needed was `file_get_contents("php://input")`? That's why I wanted to know *"how is the Android app trying to transfer the data to the PHP script"*. – Felix Kling Dec 05 '13 at 07:57
  • Sorry Felix for not providing proper information. The UI did not provide enough information! – Ashik Basheer Dec 06 '13 at 17:52

1 Answers1

1

You should use the PHP function json_encode

<?php
$var = array('a' => 1, 'b' => 2);

echo json_encode($var);
?>

You can Decode in same way by same way json_decode

$var= '{"car": 5}';

$obj = json_decode($var);
echo $obj->{'car'}; 
StaticVariable
  • 5,253
  • 4
  • 23
  • 45