0

I'm trying to decode JSON format

What I am sending is:

{
"id": 123,
"name": "John",
“surname”: “Smith”,
“department”: 3
}

I am sending POST with data via Postman, in the picture.
So, this is the data I want to decode:

"data_serever_received": "{ \"id\": 123, \"name\": \"john\", “surname”: “Smith”, “department”: 3 }"

enter image description here

I tried

$this->input->data["id"]

but it is not working.

How can I get the id, name, surname and etc. values?

5
  • the output named user on the picture is $this->input->data Commented Nov 15, 2014 at 17:12
  • Except using the correct quotes instead, right? Commented Nov 15, 2014 at 17:13
  • I suggest you also use Content-Type: application/json to make the process better. Commented Nov 15, 2014 at 17:14
  • Those aren't the right quotes (") Commented Nov 15, 2014 at 17:15
  • why you are sending “surname” , “Smith” and “department” in ” instead of " ? Commented Nov 15, 2014 at 17:53

2 Answers 2

3

Your JSON is invalid and are not ".

(Zoom in with your browser if you can't see the difference).

You need to start with valid JSON before you can parse it.

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

Comments

0

You can use json_decode to make it an array.

$json_array = json_decode($json_object);//$json_object will need to contain your json string.

then you can access like this:

echo $json_array["data"][“surname”];

PHP Doc on json_decode: http://php.net/manual/en/function.json-decode.php

5 Comments

Thanks for the answer, but when i do that i get null in $json_array
what is the $_POST variable that the json string is in?
$_POST looks like this: "POST": { "user": "{ \"id\": 123, \"name\": \"john\", “surname”: “Smith”, “department”: 3 }" }
Ok, and you are doing json_decode($_POST['your_json_string'])?
Will you add your complete code to the question please.

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.