
Zach Rattner
Ich baue ein PHP-Skript, das JSON-Daten an ein anderes Skript weitergibt. Mein Skript baut Daten in ein großes assoziatives Array ein und gibt die Daten dann mit aus json_encode
. Hier ist ein Beispielskript:
$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: text/javascript');
echo json_encode($data);
Der obige Code ergibt die folgende Ausgabe:
{"a":"apple","b":"banana","c":"catnip"}
Das ist großartig, wenn Sie eine kleine Datenmenge haben, aber ich würde etwas in dieser Richtung bevorzugen:
{
"a": "apple",
"b": "banana",
"c": "catnip"
}
Gibt es eine Möglichkeit, dies in PHP ohne einen hässlichen Hack zu tun? Es scheint, als ob jemand an Facebook herausgefunden.

awhie29urh2
PHP 5.4 bietet die JSON_PRETTY_PRINT
Option zur Verwendung mit der json_encode()
Anruf.
http://php.net/manual/en/function.json-encode.php
<?php
...
$json_string = json_encode($data, JSON_PRETTY_PRINT);

Kendall Hopkins
Diese Funktion nimmt eine JSON-Zeichenfolge und rückt sie sehr gut lesbar ein. Es sollte auch konvergent sein,
prettyPrint( $json ) === prettyPrint( prettyPrint( $json ) )
Eingang
{"key1":[1,2,3],"key2":"value"}
Ausgabe
{
"key1": [
1,
2,
3
],
"key2": "value"
}
Code
Funktion prettyPrint( $json ) { $result=""; $level = 0; $in_quotes = falsch; $in_escape = falsch; $ends_line_level = NULL; $json_length = strlen( $json ); for( $i = 0; $i < $json_length; $i++ ) { $char = $json[$i]; $new_line_level = NULL; $post = ""; if( $ends_line_level !== NULL ) { $new_line_level = $ends_line_level; $ends_line_level = NULL; } if ( $in_escape ) { $in_escape = falsch; } else if( $char === '"' ) { $in_quotes = !$in_quotes; } else if( ! $in_quotes ) { switch( $char ) { case '}': case ']': $level-- ; $ends_line_level = NULL; $new_line_level = $level; break; case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
Many users suggested that you use
echo json_encode($results, JSON_PRETTY_PRINT);
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.
header('Content-Type: application/json');
This will result in a well formatted output.
Or, if you like extensions you can use JSONView for Chrome.

Mike
I realize this question is asking about 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):
$json = json_encode(json_decode($json), JSON_PRETTY_PRINT);
Example:
header('Content-Type: application/json');
$json_ugly = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$json_pretty = json_encode(json_decode($json_ugly), JSON_PRETTY_PRINT);
echo $json_pretty;
This outputs:
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5
}

Jason
I had the same issue.
Anyway I just used the json formatting code here:
http://recursive-design.com/blog/2008/03/11/format-json-with-php/
Works well for what I needed it for.
And a more maintained version: https://github.com/GerHobbelt/nicejson-php
Gluing several answers together fit my need for existing json:
Code:
echo "<pre>";
echo json_encode(json_decode($json_response), JSON_PRETTY_PRINT);
echo "</pre>";
Output:
{
"data": {
"token_type": "bearer",
"expires_in": 3628799,
"scopes": "full_access",
"created_at": 1540504324
},
"errors": []"pagination": {}, "token_type": "bearer", "expires_in": 3628799, "scopes": "full_access", "created_at": 1540504324 }
Ich habe dies verwendet:
echo "<pre>".json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)."</pre>";
Oder verwenden Sie PHP-Header wie folgt:
header('Content-type: application/json; charset=UTF-8');
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
9897700cookie-checkPretty-Printing-JSON mit PHPyes
Für PHP vor 5.4 können Sie den Fallback in verwenden upgradephp als
up_json_encode($data, JSON_PRETTY_PRINT);
– mario
6. Februar 2013 um 22:28 Uhr
Gebrauch von header(‘Content-Type: application/json’); macht Browser schön drucken
– partho
23. März 2018 um 17:09 Uhr
Ab Juli 2018, nur durch Senden der
Content-Type: application/json
Header Firefox zeigt das Ergebnis mit seinem eigenen internen JSON-Parser an, während Chrome den Klartext anzeigt. +1 Firefox!– andreszs
8. Juli 2018 um 1:38 Uhr