Php JSON解码/编码丢失换行符/文件结构?
此php脚本向其中一个JSON对象(数组)添加一个元素Php JSON解码/编码丢失换行符/文件结构?,php,json,Php,Json,此php脚本向其中一个JSON对象(数组)添加一个元素 #/usr/bin/php 不幸的是没有 由于数据是供机器使用的,所以这不应该是一个大问题——如果您想直观地检查数据,有许多工具可以根据上下文自动进行打印。不幸的是,没有 由于数据是供机器使用的,所以这不应该是一个大问题——如果您想目视检查数据,有许多工具可以根据上下文自动进行漂亮的打印。使用打印\r: $pretty_json = print_r(json_decode($arr, true), true); 意识到您确实需要json文
#/usr/bin/php
不幸的是没有
由于数据是供机器使用的,所以这不应该是一个大问题——如果您想直观地检查数据,有许多工具可以根据上下文自动进行打印。不幸的是,没有
由于数据是供机器使用的,所以这不应该是一个大问题——如果您想目视检查数据,有许多工具可以根据上下文自动进行漂亮的打印。使用打印\r
:
$pretty_json = print_r(json_decode($arr, true), true);
意识到您确实需要json文件,请检查使用print\r
:
$pretty_json = print_r(json_decode($arr, true), true);
意识到您确实需要json文件,请检查为什么格式很重要?当然,这是为了让程序/进程消费,从而消除对人类可读性的需求
我有一个方便的google chrome扩展来查看json提要:
为什么格式很重要?当然,这是为了让程序/进程消费,从而消除对人类可读性的需求
我有一个方便的google chrome扩展来查看json提要:
使用Denis Ermolin提供的,并添加了str_replace():
使用了Denis Ermolin提供的,并添加了str_replace():
很抱歉指出显而易见的问题,但是print\r()
不会生成JSON。该函数看起来还可以,但似乎没有引号中的斜杠。这种行为正确吗?Jsonformatter不接受print\r函数。如果可能的话,请帮助我解决这个问题。很抱歉声明了一个明显的问题,但是print\r()
不生成JSON。该函数看起来不错,但似乎在引号中没有斜杠。这种行为正确吗?Jsonformatter不接受print\r函数。如果可能,请帮助我解决这个问题。是否有一个CLI/PHP工具可以很好地格式化json文件?添加元素后,我可以在文件上运行该命令。是否有CLI/PHP工具可以很好地格式化json文件?我可以在添加元素后在文件上运行该命令。
{"name":"symfony\/framework-standard-edition","description":"The \"Symfony Standard Edition\" distribution","autoload":{"psr-0":{"_empty_":"src\/"}},"require":{"php":">=5.3.3","symfony\/symfony":"2.1.*","doctrine\/orm":">=2.2.3,<2.4-dev","doctrine\/doctrine-bundle":"1.0.*","twig\/extensions":"1.0.*","symfony\/assetic-bundle":"2.1.*","symfony\/swiftmailer-bundle":"2.1.*","symfony\/monolog-bundle":"2.1.*","sensio\/distribution-bundle":"2.1.*","sensio\/framework-extra-bundle":"2.1.*","sensio\/generator-bundle":"2.1.*","jms\/security-extra-bundle":"1.2.*","jms\/di-extra-bundle":"1.1.*","friendsofsymfony\/user-bundle":"*"},"scripts":{"post-install-cmd":["Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"],"post-update-cmd":["Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets","Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"]},"minimum-stability":"dev","extra":{"symfony-app-dir":"app","symfony-web-dir":"web"}}
$pretty_json = print_r(json_decode($arr, true), true);
<?php
$filename = 'composer.json';
$obj = json_decode(file_get_contents($filename, true));
if (null === $obj) {
throw new Exception(json_last_error()); // this will just be an error code
}
$obj->require = (object) array_merge((array) $obj->require, array('friendsofsymfony/user-bundle' => "*"));
file_put_contents($filename,pretty_json(json_encode($obj)));
function pretty_json($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
$result = str_replace("\\/", "/", $result);
return $result;
}
?>