0

Anyway knows about json encoding with totally arrange like this https://graph.facebook.com/4 like facebook graph?

coz when i write json simply php echo json_encode this will come out {"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"https://www.facebook.com/zuck","username":"zuck","gender":"male","locale":"en_US"}

just a straight line... :D anyone knows about it? :D please share

hakre
  • 193,403
  • 52
  • 435
  • 836
Hannibal Burr
  • 49
  • 1
  • 7
  • You find json encoding documented here: http://json.org/ – hakre Apr 29 '12 at 11:45
  • possible duplicate of [PHP "pretty print" json_encode](http://stackoverflow.com/questions/7097374/php-pretty-print-json-encode) – sg3s Apr 29 '12 at 11:47

2 Answers2

1

For PHP >=5.4.0, you can use JSON_PRETTY_PRINT option when using json_encode

For PHP < 5.4.0, you can use code from a user contributed note in this page of PHP manual http://www.php.net/manual/en/function.json-encode.php#80339

<?php 
// Pretty print some JSON 
function json_format($json) 
{ 
    $tab = "  "; 
    $new_json = ""; 
    $indent_level = 0; 
    $in_string = false; 

    $json_obj = json_decode($json); 

    if($json_obj === false) 
        return false; 

    $json = json_encode($json_obj); 
    $len = strlen($json); 

    for($c = 0; $c < $len; $c++) 
    { 
        $char = $json[$c]; 
        switch($char) 
        { 
            case '{': 
            case '[': 
                if(!$in_string) 
                { 
                    $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1); 
                    $indent_level++; 
                } 
                else 
                { 
                    $new_json .= $char; 
                } 
                break; 
            case '}': 
            case ']': 
                if(!$in_string) 
                { 
                    $indent_level--; 
                    $new_json .= "\n" . str_repeat($tab, $indent_level) . $char; 
                } 
                else 
                { 
                    $new_json .= $char; 
                } 
                break; 
            case ',': 
                if(!$in_string) 
                { 
                    $new_json .= ",\n" . str_repeat($tab, $indent_level); 
                } 
                else 
                { 
                    $new_json .= $char; 
                } 
                break; 
            case ':': 
                if(!$in_string) 
                { 
                    $new_json .= ": "; 
                } 
                else 
                { 
                    $new_json .= $char; 
                } 
                break; 
            case '"': 
                if($c > 0 && $json[$c-1] != '\\') 
                { 
                    $in_string = !$in_string; 
                } 
            default: 
                $new_json .= $char; 
                break;                    
        } 
    } 

    return $new_json; 
} 
?>
chalet16
  • 197
  • 2
  • this one is right :) thank you so much. :) whew i been looking for json pretty print but not working on mine, but i upgrade the new version of php then it work.. :) thank you man.. :D – Hannibal Burr Apr 29 '12 at 11:55
0

If you are using FireFox, this might help!

http://jsonview.com/

Or just use this...

http://jsbeautifier.org/

Hope these help!

Scott F.
  • 13
  • 2