如何使用PHP格式化.txt文件上的.json数据?

edg*_*foe 0 php json

我正在使用将数组转换为JSON格式json_encode.完成此操作后,我希望至少以可读的方式将此数据保存在文本文件中.这是我到目前为止所拥有的:

$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);
Run Code Online (Sandbox Code Playgroud)

不幸的是各自的.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"}}
Run Code Online (Sandbox Code Playgroud)

正如你所看到它是非常难以理解的,而且我想在这个.json文件上创建一个索引,其中每个类型都有各自的频率和推文ID,我也想按类型对这个索引进行排序,我不想我认为这种格式很容易实现.

我希望得到这样的东西:

{"0":{
        "type":"RT",
        "frequency":1,
        "tweet_id":"516575570168385537"
  } ... etc
Run Code Online (Sandbox Code Playgroud)

有没有办法格式化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"    }
Run Code Online (Sandbox Code Playgroud)

它仿佛缩进然而,发生线路没有被跳过,以适当的方式输出:

{
    "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"
    }
Run Code Online (Sandbox Code Playgroud)

这是我执行结果的代码:

$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 );
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?

eli*_*ide 6

你说你不想"用大括号和所有'0','1'......值阻塞我的索引文件." 嗯,这就是JSON的定义结构.我不确定你期望与众不同.

也就是说,你可以让它更容易阅读.如果您正在寻找更漂亮的布局,使用换行符和缩进,请使用JSON_PRETTY_PRINT常量:

$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);
Run Code Online (Sandbox Code Playgroud)