0

I have passed JSON encoded parameters by POST which we have captured and decoded in another PHP file. I have used the following code to do that.

$entityBody = file_get_contents('php://input');
$entityBody = json_decode($entityBody, true);

I have passed the JSON encoded parameters as follows:

{
    "id": "5",
    "name": "abcd",
    "imei": "1234"
}

Here my code works perfectly fine. However, I want to get all the parameters into a single object so that we can store them efficiently because otherwise there will be too many ifs and elses to get each parameter. So I have encoded the parameters as follows:

device = {
    "id": "5",
    "name": "abcd",
    "imei": "1234"
}

But it is not working. Being new to JSON and PHP I do not know how to handle such cases. How can I achieve this?

4
  • The last "json" example is not valid JSON. Commented Jul 23, 2013 at 10:44
  • Are you setting a POST field named "device", or is that second code block the entire body? Commented Jul 23, 2013 at 10:47
  • you need to get valid json as said andre, then you decode into array with no problems Commented Jul 23, 2013 at 10:51
  • I don't get it, could please post the code you use to send the data? Commented Jul 23, 2013 at 10:54

2 Answers 2

1

use json_decode($_POST['device'], true) since your actually passing a parameter called 'device' to the php file.

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

Comments

0

You should pass json objects as follow:

 {"device" :  {
 "id": "5",
 "name": "abcd",
 "imei": "1234"
}}

or if you have an array of devices

 {"device" :  [{
 "id": "5",
 "name": "abcd",
 "imei": "1234"}
]}

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.