We build a PHP script that feeds JSON data to another script. ur script builds data into a large associative array, and then outputs the data using json_encode.
Here is an example script:
[pastacode lang=”php” manual=”%24data%20%3D%20array(‘a’%20%3D%3E%20’apple’%2C%20’b’%20%3D%3E%20’banana’%2C%20’c’%20%3D%3E%20’catnip’)%3B%0Aheader(‘Content-type%3A%20text%2Fjavascript’)%3B%0Aecho%20json_encode(%24data)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]- The above code yields the following output:
- This is great if you have a small amount of data, but I’d prefer something along these lines:
- Is there a way to do this in PHP without an ugly hack? It seems like someone at Facebook figured it out.
- php json pretty-print
PHP 5.4 offers the JSON_PRETTY_PRINT option for use with the json_encode() call.
http://php.net/manual/en/function.json-encode.php
[pastacode lang=”php” manual=”%3C%3Fphp%0A…%0A%24json_string%20%3D%20json_encode(%24data%2C%20JSON_PRETTY_PRINT)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]This function will take JSON string and indent it very readable. It also should be convergent,
[pastacode lang=”php” manual=”prettyPrint(%20%24json%20)%20%3D%3D%3D%20prettyPrint(%20prettyPrint(%20%24json%20)%20)%0A” message=”Php Code” highlight=”” provider=”manual”/]Input
[pastacode lang=”php” manual=”%7B%22key1%22%3A%5B1%2C2%2C3%5D%2C%22key2%22%3A%22value%22%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]Output
[pastacode lang=”php” manual=”%7B%0A%20%20%20%20%22key1%22%3A%20%5B%0A%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%202%2C%0A%20%20%20%20%20%20%20%203%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22key2%22%3A%20%22value%22%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]Code:
[pastacode lang=”php” manual=”function%20prettyPrint(%20%24json%20)%0A%7B%0A%20%20%20%20%24result%20%3D%20”%3B%0A%20%20%20%20%24level%20%3D%200%3B%0A%20%20%20%20%24in_quotes%20%3D%20false%3B%0A%20%20%20%20%24in_escape%20%3D%20false%3B%0A%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%24json_length%20%3D%20strlen(%20%24json%20)%3B%0A%0A%20%20%20%20for(%20%24i%20%3D%200%3B%20%24i%20%3C%20%24json_length%3B%20%24i%2B%2B%20)%20%7B%0A%20%20%20%20%20%20%20%20%24char%20%3D%20%24json%5B%24i%5D%3B%0A%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%24post%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20if(%20%24ends_line_level%20!%3D%3D%20NULL%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20%24ends_line_level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(%20%24in_escape%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_escape%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20if(%20%24char%20%3D%3D%3D%20’%22’%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_quotes%20%3D%20!%24in_quotes%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20if(%20!%20%24in_quotes%20)%20%7B%0A” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”switch(%20%24char%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20’%7D’%3A%20case%20’%5D’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24level–%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20%24level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20’%7B’%3A%20case%20’%5B’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24level%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20’%2C’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20%24level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20’%3A’%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24post%20%3D%20%22%20%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22%20%22%3A%20case%20%22%5Ct%22%3A%20case%20%22%5Cn%22%3A%20case%20%22%5Cr%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24char%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20%24new_line_level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%20else%20if%20(%20%24char%20%3D%3D%3D%20’%5C%5C’%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_escape%20%3D%20true%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%20%7D%0A%20%20%20%20%20%20%20%20if(%20%24new_line_level%20!%3D%3D%20NULL%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24result%20.%3D%20%22%5Cn%22.str_repeat(%20%22%5Ct%22%2C%20%24new_line_level%20)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%24result%20.%3D%20%24char.%24post%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20return%20%24result%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]We just used the json formatting code here:
http://recursive-design.com/blog/2008/03/11/format-json-with-php/
[pastacode lang=”php” manual=”echo%20json_encode(%24results%2C%20JSON_PRETTY_PRINT)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]Which is absolutely right. But it’s not enough, the browser needs to understand the type of data, you can specify the header just before echo-ing the data back to the user.
[pastacode lang=”php” manual=”header(‘Content-Type%3A%20application%2Fjson’)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]- This will result in a well formatted output.
- Or, if you like extensions you can use JSONView for Chrome.
Simple way for php>5.4:
[pastacode lang=”php” manual=”%24Data%20%3D%20array(‘a’%20%3D%3E%20’apple’%2C%20’b’%20%3D%3E%20’banana’%2C%20’c’%20%3D%3E%20’catnip’)%3B%0A%24json%3D%20json_encode(%24Data%2C%20JSON_PRETTY_PRINT)%3B%0Aheader(‘Content-Type%3A%20application%2Fjson’)%3B%0Aprint_r(%24json)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]Result in browser
[pastacode lang=”php” manual=”%7B%0A%20%20%20%20%22a%22%3A%20%22apple%22%2C%0A%20%20%20%20%22b%22%3A%20%22banana%22%2C%0A%20%20%20%20%22c%22%3A%20%22catnip%22%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]How to encode an associative array to a pretty-formatted JSON string, so this doesn’t directly answer the question, but if you have a string that is already in JSON format, you can make it pretty simply by decoding and re-encoding it (requires PHP >= 5.4):
[pastacode lang=”php” manual=”%24json%20%3D%20json_encode(json_decode(%24json)%2C%20JSON_PRETTY_PRINT)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
easy explanation
easy to understand