-2

I tried to add the simple json below to my array in my API

$result = array();
...
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$json =  json_encode($arr);               
$result['json'] = $json;

return $result;

But after add it to my array, it loose the format like this.

Please look at the red arrow point to and compare with screenshot 2.

enter image description here
What I want is my json should display like

enter image description here

How can I prevent it. Any help or suggestion would be great appreciated

Linh
  • 57,942
  • 23
  • 262
  • 279
  • What are you expecting it to be? You've taken an *associative* array and turned it into a JSON string (which defines an "object" in JSON terminology, because it's an associative array, not a numeric-index array), then added that string to you result. – T.J. Crowder Sep 25 '16 at 10:28
  • @T.J.Crowder I have update my question. please help me, i'm very new in PHP, I have searched but still don't find the answer – Linh Sep 25 '16 at 10:31
  • Both screenshots show equivalent JSON strings. What is the problem? Surely you are not going to occupy yourself with white-space? And that the double quotes are escaped in the first screenshot, is only normal since the whole string is quoted in double quotes, it has to be like that, but the string itself does not *store* those backslashes... it is notation only. – trincot Sep 25 '16 at 10:33
  • Possible duplicate of [Pretty-Printing JSON with PHP](http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php) – trincot Sep 25 '16 at 10:34
  • @trincot I have update my question, please check it again. thank you so much – Linh Sep 25 '16 at 10:43
  • Your latest screenshot shows a property called `json` which is **not** JSON. JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247) What you have there is just an object or an associative array within an overall JSON structure, and [Heinrich](http://stackoverflow.com/a/39685892/157247) is correct about what's going on. – T.J. Crowder Sep 25 '16 at 10:43
  • thank you for your explain, I will look at this. and the solution add array to array solve my problem – Linh Sep 25 '16 at 10:48

2 Answers2

2

What seems to be happening here is double encoding, your $result array gets encoded as well, then it encodes the $result['json'] again, causing the output you are seeing.

$result = array();
...
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$json =  json_encode($arr);               
$result['json'] = $json;

return $result; // either you are using a framework, or not showing a step, but this also seems to be encoded before being sent back to the client.

Given what I can infer from you have then shown us, do not encode your array before assigning it to $result['json']

$result = array();
...          
$result['json'] = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

return $result;

Should then give you what you want.

Heinrich Henning
  • 933
  • 5
  • 15
0
$result = array();
$result['json']= array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$json =  json_encode($result);               

return $json;

this solution is working for me .