I\'m developing an Android app, and for the API I\'m sending my requests to a URL that should return JSON data.
This is what I\'m getting in my output:
Your problem is actually very easy to solve. The Chrome JSON Formatter plugin only formats your output if the Content-Type header is set to application/json.
The only thing you need to change in your code is to use header('Content-Type: application/json');
in your PHP code, before returning the json-encoded data.
Your JSON response is already in good shape. The reponse you are viewing is in Chrome browser. Thats why it looks like that . If you add below line in yoru code
var_dump($response);
and then rightclick -> view source in chrome brower , you will see the tree structure. Anyway you can copy your JSON reponse and check the validy here
The same thing I belive first when I saw it in chrome . It happens to me also.
just Add pre Tag -->
<pre><?php ---you`r code--- ?> </pre>
and
echo json_encode($response, JSON_PRETTY_PRINT);
after adding pre-Tag and JSON_PRETTY_PRINT I'm sure your Problem will solve.
PHP's json_encode function takes a second argument, for $options
. Here you can use JSON_PRETTY_PRINT
to print it like you see in the Twitter API
E.g.
echo json_encode($my_array, JSON_PRETTY_PRINT);
All you have to do is to add a second argument to the json_encode
method like this.
echo json_encode($response_date, JSON_PRETTY_PRINT);
or
print json_encode($response_date, JSON_PRETTY_PRINT);
You can visit this link to read more on how to use json_encode
.
Hope it helps.
Your JSON should be like your first image; All in one line squished together. You can pass your output to jsonlint.com which can help you find your typo/bad data. ALL the current other answers could help you solve your issue as well. they all give you different options to format your output if you really want to do that.
Here's another way for you to display your JSON in HTML.
If you wrap your response in HTML < pre > tags you'll keep the white space format.
For example, I use jQuery to get some json data and in jQuery take the json object and put it directly in a < pre > that's formatted
<pre class="output" style="text-align:left; width:80%; margin:0 auto">[ results ]</pre>
I use
$('.output').html( JSON.stringify(data,null,4) );
This will take your json return, format it with spaces with the HTML < pre > can use to format it like you want to see.
If you want to have your json color coded... you can use http://prismjs.com/ or any other highlighting system...