Php 返回json内容格式的文件
我有一个具有以下结构的txt文件:Php 返回json内容格式的文件,php,json,Php,Json,我有一个具有以下结构的txt文件: 17/02/2016 9:50 [info] "hello" 17/02/2016 10:20 [debug] "world" 现在我试着用以下方法来阅读: $fh = fopen($this->_logPath, "r"); $content = array(); while ($line = fgets($fh)) { array_push($content, $line); } fclose($fh); return json_en
17/02/2016 9:50 [info] "hello"
17/02/2016 10:20 [debug] "world"
现在我试着用以下方法来阅读:
$fh = fopen($this->_logPath, "r");
$content = array();
while ($line = fgets($fh))
{
array_push($content, $line);
}
fclose($fh);
return json_encode($content);
文件读取正确,但在我的chrome扩展名try Rest API中,我在json选项卡中得到:
意外字符串
如何返回json内容中的每一行?例如,这样的结果:
"trace": {
info: {
date: {
"17/02/2016 9:50" {
-"content": "hello"
}
}
}
debug: {
date: {
"17/02/2016 10:20" {
-"content": "world"
}
}
}
}
{
"trace":[
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
},
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
}
]
}
或者如果有更好的组织,我会很高兴看到。我会选择这样的结构:
"trace": {
info: {
date: {
"17/02/2016 9:50" {
-"content": "hello"
}
}
}
debug: {
date: {
"17/02/2016 10:20" {
-"content": "world"
}
}
}
}
{
"trace":[
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
},
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
}
]
}
请注意,跟踪包含一个logitem数组。
要分析您的文件,应使用以下方法:
$fh = fopen($this->_logPath, "r");
$content = array();
$content["trace"] = array();
while ($line = fgets($fh))
{
$raw = preg_split("/[\[\]]/", $line); //splits string at [ and ], results in an array with three items
$entry = array();
$entry["date"] = trim($raw[0]);
$entry["level"] = trim($raw[1]);
$entry["message"] = trim($raw[2]);
$content["trace"][] = $entry;
}
fclose($fh);
return json_encode($content);
对于json的实验,您可能会喜欢,我一直认为这非常有用 我会选择这样的结构:
"trace": {
info: {
date: {
"17/02/2016 9:50" {
-"content": "hello"
}
}
}
debug: {
date: {
"17/02/2016 10:20" {
-"content": "world"
}
}
}
}
{
"trace":[
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
},
{
"date":"17/02/2016 9:50",
"level":"debug",
"message":"hello"
}
]
}
请注意,跟踪包含一个logitem数组。
要分析您的文件,应使用以下方法:
$fh = fopen($this->_logPath, "r");
$content = array();
$content["trace"] = array();
while ($line = fgets($fh))
{
$raw = preg_split("/[\[\]]/", $line); //splits string at [ and ], results in an array with three items
$entry = array();
$entry["date"] = trim($raw[0]);
$entry["level"] = trim($raw[1]);
$entry["message"] = trim($raw[2]);
$content["trace"][] = $entry;
}
fclose($fh);
return json_encode($content);
对于json的实验,您可能会喜欢,我一直认为这非常有用 代码的上下文是什么?您正在返回JSON,但您将它返回到哪里?“chrome extension for try Rest API”-抱歉,但您到底在说什么?此外,您在代码中所做的只是构建一个列表。要获得一个像您正在显示的对象,您必须解析这些行。@jsfan我在chrome扩展的响应中返回它,实际上我想管理一个更好的json编码组织,而不是一个简单的数组打印。正如@low_rents已经提出的要求。您正在谈论的Chrome扩展是什么?代码的上下文是什么?您正在返回JSON,但您将它返回到哪里?“chrome extension for try Rest API”-抱歉,但您到底在说什么?此外,您在代码中所做的只是构建一个列表。要获得一个像您正在显示的对象,您必须解析这些行。@jsfan我在chrome扩展的响应中返回它,实际上我想管理一个更好的json编码组织,而不是一个简单的数组打印。正如@low_rents已经提出的要求。你说的Chrome扩展名是什么?很好的代码,只是在这段时间做了一点修改,
$raw
而不是raw
,还有日期$raw[1]
,但这取决于我如何格式化文件。有一个问题:为什么这行(“/[\[\]]/
在PHPSTORM中以红色下划线?在我的电脑上不是。我正在使用PHPSTORM 10.0.3,可能你有一个旧版本吗?不,我也有10.0.3版本:\n当我在上面按住鼠标时,没有文本出现,就像这样:奇怪!可能PHPSTORM无法识别正确的php版本?在我的电脑上,一切看起来都很好:代码不错,只是有点正确在此期间,$raw
而不是raw
,并获取日期$raw[1]
,但这取决于我如何格式化文件。一个问题:为什么这行(“/[\[\]]/
在PHPSTORM中用红色下划线?在我的电脑上不是。我正在使用PHPSTORM 10.0.3,可能你有一个旧版本?不,我也有10.0.3版本:\n当我在上面按住鼠标时,没有文本出现,就像这样:奇怪!可能PHPSTORM无法识别正确的php版本?在我的电脑上,一切看起来都很好: