我正在使用json_encode
将数组转换为JSON格式。完成此操作后,我希望至少以可读的方式将此数据保存在文本文件中。这就是我到目前为止所拥有的:
$keywords = parseTweet ( $tweet, $tweet_id );
// print_r ( $keywords );
$json = json_encode ( $keywords, JSON_FORCE_OBJECT );
print_r ( $json );
$fp = fopen('index.json', 'w');
fwrite($fp, $json);
fclose($fp);
不幸的是,相应的.json文件(打开它为.txt)各自有:
{"0":{"type":"RT","frequency":1,"tweet_id":"516575570168385537"},"1":
{"type":"iGamingSummit","frequency":1,"tweet_id":"516575570168385537"},"2":
{"type":"CEO","frequency":1,"tweet_id":"516575570168385537"},"5":
{"type":"guest","frequency":1,"tweet_id":"516575570168385537"},"6":
{"type":"keynote","frequency":1,"tweet_id":"516575570168385537"},"7":
{"type":"speaker","frequency":1,"tweet_id":"516575570168385537"},"8":
{"type":"SiGMA2014","frequency":1,"tweet_id":"516575570168385537"},"9":
{"type":"http","frequency":1,"tweet_id":"516575570168385537"},"10":
{"type":"t","frequency":1,"tweet_id":"516575570168385537"},"11":
{"type":"co","frequency":1,"tweet_id":"516575570168385537"},"12":
{"type":"n5hPpTV1bH","frequency":1,"tweet_id":"516575570168385537"}}
正如你所看到的那样它是不可读的,而且我想在这个.json文件上创建一个索引,其中每个类型都出现各自的频率和推文ID,我也想按类型对这个索引进行排序,我不认为这种格式很容易实现这一点。
我希望得到这样的东西:
{"0":{
"type":"RT",
"frequency":1,
"tweet_id":"516575570168385537"
} ... etc
有没有办法格式化JSON以使其更具可读性,或者我是否应该重新考虑使用它,也许可以尝试使用CSV或更好的东西,它不会用大括号和所有"来阻塞我的索引文件。 0"," 1" ...值?
--------更新---------
我尝试在第一个答案中使用该建议,但现在这里是文本文件的样子:
{ "0": { "type": "app", "frequency": 1, "tweet_id": "561522539340771328" }, "1": { "type": "cat", "frequency": 1, "tweet_id": "561522673805975553" }, "2": { "type": "dog", "frequency": 1, "tweet_id": "561522539340771328" }, "3": { "type": "Deed", "frequency": 1, "tweet_id": "561522453118464000" }, "4": { "type": "deep", "frequency": 1, "tweet_id": "542089120651440129" }, "5": { "type": "dig", "frequency": 1, "tweet_id": "535817228927901696" }
就好像缩进 一样,但是没有以正确的方式跳过这一行输出:
{
"0": {
"type": "app",
"frequency": 1,
"tweet_id": "561522539340771328"
},
"1": {
"type": "cat",
"frequency": 1,
"tweet_id": "561522673805975553"
},
"2": {
"type": "dog",
"frequency": 1,
"tweet_id": "561522539340771328"
},
"3": {
"type": "Deed",
"frequency": 1,
"tweet_id": "561522453118464000"
},
"4": {
"type": "deep",
"frequency": 1,
"tweet_id": "542089120651440129"
},
"5": {
"type": "dig",
"frequency": 1,
"tweet_id": "535817228927901696"
}
这是我执行结果的代码:
$json_index = array_values($json_index);
$json = json_encode ( $json_index, JSON_FORCE_OBJECT|JSON_PRETTY_PRINT );
print_r($json);
$index = fopen ( $index_path, 'w' );
fwrite ( $index, $json);
fclose ( $index );
我做错了什么?
答案 0 :(得分:6)
你说你不想“用大括号和所有'0','1'......值来阻塞我的索引文件。”嗯,这就是JSON的定义结构。我不确定你期望与众不同。
那就是说,你可以让它更容易阅读。如果您正在寻找更漂亮的布局,包括换行符和缩进,请使用JSON_PRETTY_PRINT
constant:
$keywords = parseTweet ( $tweet, $tweet_id );
// print_r ( $keywords );
$json = json_encode ( $keywords, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT );
print_r ( $json );
$fp = fopen('index.json', 'w');
fwrite($fp, $json);
fclose($fp);
答案 1 :(得分:2)
你的意思是你想要一个数组 JSON吗?像那样:
[
{
"type":"RT",
"frequency":1,
"tweet_id":"516575570168385537"
},
{
"type":"iGamingSummit",
"frequency":1,
"tweet_id":"516575570168385537"
},
{
"type":"CEO",
"frequency":1,
"tweet_id":"516575570168385537"
}
]
如果是这样,您只需使用JSON_PRETTY_PRINT
常量并确保您的推文数组使用array_values
包含数字键。
$keywords = array_values($keywords);
$json = json_encode ( $keywords, JSON_PRETTY_PRINT );
如果我从你的样本中获取对象JSON来测试它:
<?php
$keywords = json_decode('{"0":{"type":"RT","frequency":1,"tweet_id":"516575570168385537"},"1":{"type":"iGamingSummit","frequency":1,"tweet_id":"516575570168385537"},"2":{"type":"CEO","frequency":1,"tweet_id":"516575570168385537"},"5":{"type":"guest","frequency":1,"tweet_id":"516575570168385537"},"6":{"type":"keynote","frequency":1,"tweet_id":"516575570168385537"},"7":{"type":"speaker","frequency":1,"tweet_id":"516575570168385537"},"8":{"type":"SiGMA2014","frequency":1,"tweet_id":"516575570168385537"},"9":{"type":"http","frequency":1,"tweet_id":"516575570168385537"},"10":{"type":"t","frequency":1,"tweet_id":"516575570168385537"},"11":{"type":"co","frequency":1,"tweet_id":"516575570168385537"},"12":{"type":"n5hPpTV1bH","frequency":1,"tweet_id":"516575570168385537"}}');
/*
* Force object to array
*/
$arrayValues = array();
foreach($keywords as $value) {
$arrayValues[] = $value;
}
// You must have at least PHP 5.4
$json = json_encode($arrayValues, JSON_PRETTY_PRINT);
$index = fopen(dirname(__FILE__).'/test.txt', 'w' );
fwrite ($index, $json);
fclose ($index);
我的test.txt文件包含:
[
{
"type": "RT",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "iGamingSummit",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "CEO",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "guest",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "keynote",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "speaker",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "SiGMA2014",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "http",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "t",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "co",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "n5hPpTV1bH",
"frequency": 1,
"tweet_id": "516575570168385537"
}
]
答案 2 :(得分:0)
将其编写为CSV会使其非常具有人性化的可读性。这似乎是你的意图。
直接来自PHP.net fputscsv http://php.net/manual/en/function.fputcsv.php
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
以上示例将以下内容写入file.csv:
aaa,bbb,ccc,dddd
123,456,789
答案 3 :(得分:0)
使用上面的例子,我解释了你的例子,然后重新编码并将其写入文件,似乎是合法的,输出为:
[
{
"type": "RT",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "iGamingSummit",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "CEO",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "guest",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "keynote",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "speaker",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "SiGMA2014",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "http",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "t",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "co",
"frequency": 1,
"tweet_id": "516575570168385537"
},
{
"type": "n5hPpTV1bH",
"frequency": 1,
"tweet_id": "516575570168385537"
}
]
使用您的示例代码,如下所示:
$keywords = parseTweet ( $tweet, $tweet_id );
file_put_contents("index.json",
json_encode(
array_values($keywords),
JSON_PRETTY_PRINT
)
);
请记住,如果您将此输出读回PHP,PHP将忽略格式化,这纯粹是为了在文本编辑器或vim中打开文件。