17

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: My response

And I'd like it to be displayed as Twitter response:

Twitter's JSON response

I'm assuming my response is not being parsed by the JSON Formatter Chrome extension because it's encoded badly, thus my app can't get the values I need.

Here's my PHP code:

<?php

$response = array();

if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) 
{

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['decription'];

    require_once __DIR__ . '/db_connect.php';

    $db = new DB_CONNECT();

    $result = mysql_query("INSER INTO products(name, price, description) VALUES('$name', '$price', '$description')");

    if ($result) {
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        echo json_encode($response);
    } else {

        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred!";

        echo json_encode($response);
        }
} else {

    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    echo json_encode($response);

}

?>

I want to know how to display the JSON data correctly so that JSON Formatter and my android App can parse it correctly.

4
  • probably a typo but your Insert query start with INSER. Commented May 17, 2013 at 1:46
  • It would be infinitely more helpful if you posted your actual result JSON, as opposed to an image. Commented May 17, 2013 at 1:53
  • I am not very familiar with JSON output by PHP, but I'm curious that I can see a "raw" and "parsed" function in your second screenshot, isn't that some javascript function that is actually formatting the output? So say you do a pretty print, wouldn't that increase your page data size and it should not be what the users want? Commented May 17, 2013 at 1:58
  • 1
    @ChorWaiChun That's the JSON Formatter Chrome extension in action. Commented May 17, 2013 at 2:41

7 Answers 7

20

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.

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

3 Comments

I know the question specifically states chrome etc, but I found that to get this to work in FF, I had to use header('Content-Type: application/json'); instead of header('Content-Type', 'application/json'); (replace comma with a colon)
Yes, I made a typo. I only noticed just now. Thanks! ;)
@OscarSwanros, could you flag the issue as solved? Thanks! :)
5

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);

Comments

3

You can pass an argument to the json_encode function like this :

echo json_encode($response, JSON_PRETTY_PRINT);

PHP 5.2.0 => http://php.net/manual/en/function.json-encode.php

2 Comments

Apparently, JSON_PRETTY_PRINT is only available on PHP 5.4+, and the server runs 5.2.
You're right ! I found this class PRETTY_JSON http://pastebin.com/xB0fG9py. I didn't test it but you can get inspired by this one.
1

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...

Comments

0

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.

Comments

0

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.

Comments

0

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.

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.