2

This is my current PHP code

while ($row = mysql_fetch_array($result)) 
    {
        $post = new Post();
        $post->setId($row['post_id']); 
        $post->setBody($row['post_body']); 
        $post->setImage($row['post_imgurl']);
        $post->setStamp($row['post_stamp']);
        $postList[]  = $post;
    }
    return array('post'=> $postList);
}

and this is its output JSON. How can i make my JSON more readable like below and remove / the character?

More readable output:

ExtraCharacterandJSONFormat

7
  • 1
    what do you mean by "readable"? Commented Nov 15, 2013 at 17:12
  • 6
    More readable where? What's the real issue here? The / needs to be escaped in a JSON string, that's how it works (see: json.org) Commented Nov 15, 2013 at 17:12
  • 4
    If you don't escape the backslashes, it won't be valid JSON. What does it look like when you actually output the image value? Commented Nov 15, 2013 at 17:12
  • 2
    The / doesn't need to be escaped (\ does), but it does no harm if you do. PHP escapes them so that having </script> in a string literal will become <\/script> and thus won't terminate a script block if you use the JSON as JavaScript in HTML. Commented Nov 15, 2013 at 17:15
  • 1
    thank you guys. this is the current output of my JSON i.sstatic.net/Ue4Aa.png and i want to make it i.sstatic.net/Gwkz1.png :) Commented Nov 15, 2013 at 17:17

2 Answers 2

4

If you want readable JSON, then read it in a tool designed to present JSON for reading (such as the Chrome JSONView extension). Don't try to munge raw data into particular patterns.

That said, you can persuade PHP to munge the data in that fashion with:

json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Sign up to request clarification or add additional context in comments.

Comments

0

http://www.php.net/manual/en/function.json-encode.php

Please, take a look at the $options param of the json_encode function, I am sure, it solves your problem.

1 Comment

In particular, JSON_UNESCAPED_SLASHES

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.