0

I am new to PHP.

I am sending json using JsonObjectRequest in Android via POST Method.

I don't know how to retrieve that json in php and decode it. Could you help me? The json I am sending is

{"firstname":"test","lastname":"test"}

I need both the JSON objects in seperate variables.

3
  • 1
    Try json_decode('{"firstname":"test","lastname":"test"}'); Commented Mar 24, 2018 at 11:25
  • json_decode function will help you json_decode Commented Mar 24, 2018 at 11:26
  • Possible duplicate of PHP- Decode JSON Commented Mar 24, 2018 at 11:28

1 Answer 1

1

You can get this data by file_get_contents("php://input");. And then decode it by json_decode(). Try following code:

$data_json = file_get_contents("php://input");
$data = json_decode($data_json,true);
var_dump($data); //show your data

EDIT

To echo your data:

echo $data['firstname'];
echo $data['lastname'];
Sign up to request clarification or add additional context in comments.

2 Comments

How to get both of the json objects in seperate variables and echo them?
store them in variable like $firstname = $data['firstname'];

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.