Php 错误的json编码

Php 错误的json编码,php,json,Php,Json,我试图在json中解析txt文件内容。这是文件内容: [19-02-2016 16:48:45.505547] [info] System done. 0: array( 'ID' => 'Example 2' ) 下面是我解析文件的代码: $fh = fopen($file, "r"); $content = array(); $content["trace"] = array(); while ($line = fgets($fh)) { $raw = pre

我试图在json中解析txt文件内容。这是文件内容:

[19-02-2016 16:48:45.505547] [info] System done. 
 0: array(
  'ID' => 'Example 2'
 )
下面是我解析文件的代码:

$fh = fopen($file, "r");
$content = array(); 
$content["trace"] = array();

while ($line = fgets($fh))
{
     $raw = preg_split("/[\[\]]/", $line); 
     $entry = array();
     $entry["date"] = trim($raw[1]);
     $entry["type"] = trim($raw[3]);
     $entry["message"] = trim($raw[4]);
     $content["trace"][] = $entry;
}

fclose($fh);
return $content;
这是从
$content
返回的内容:

{
    "trace": [{
        "date": "19-02-2016 16:48:45.505547"
        "type": "info"
        "message": "System done."
    }, {
        "date": ""
        "type": ""
        "message": ""
    }, {
        "date": ""
        "type": ""
        "message": ""
    }, {
        "date": ""
        "type": ""
        "message": ""
    }]
}

更新我期待这个:

{
    "trace": [{
        "date": "19-02-2016 16:48:45.505547"
        "type": "info"
        "message": "System done."
        "ID": Example 2
    }]
}
如何查看数组将被视为新行,代码将在中创建其他空数组,而不包含内容。我只是想稍后创建新的索引
消息
,并放置数组内容,我如何才能做到这一点

更新文件中的更多内容

预期结果:

试试这个:

代码 Json对象(字符串):

注释:re。正则表达式:

  • 该文件作为一个整体读入字符串变量($input)
  • preg_match_all(RegEx)还扫描整个输入
  • 代码迭代所有命中,其中组包含以下部分…
    • 1:日期
    • 2:类型
    • 3:留言
    • 4:ID
  • 正则表达式的详细信息:

试试这个:

代码 Json对象(字符串):

注释:re。正则表达式:

  • 该文件作为一个整体读入字符串变量($input)
  • preg_match_all(RegEx)还扫描整个输入
  • 代码迭代所有命中,其中组包含以下部分…
    • 1:日期
    • 2:类型
    • 3:留言
    • 4:ID
  • 正则表达式的详细信息:



请给我们一个示例,说明您期望的结果。@hherger看到我的更新了吗?是否存在跟踪将有多个对象的情况?如果有,请发布源txt文件和预期结果的示例。@EdsonHoracioJunior complete example请向我们展示一个您预期结果的示例。@hherger看到我的更新了吗?是否存在跟踪将有多个对象的情况?如果有,请发布源txt文件的示例和预期结果。@EdsonHoracioJunior complete exampleNope,结果不在json内容中。。我发布了一个示例,您之前的示例是一个php数组。我把它添加到我的答案中。我不明白正则表达式是如何工作的。。你能解释一下吗?更新:如果我没有阵列,那么就只完成这个:
[19-02-2016 16:48:45.505547][info]系统未返回任何内容@谢谢正则表达式的解释,我可以插入一个条件吗?查看我的上一条评论以了解问题,可能您没有看到。您是否混合了单行条目(带数组…)和多行条目(带数组…)?不,结果不在json内容中。。我发布了一个示例,您之前的示例是一个php数组。我把它添加到我的答案中。我不明白正则表达式是如何工作的。。你能解释一下吗?更新:如果我没有阵列,那么就只完成这个:
[19-02-2016 16:48:45.505547][info]系统未返回任何内容@谢谢正则表达式的解释,我可以插入一个条件吗?查看我的上一条评论以查看问题,可能您没有看到。您是否混合了单行条目(带数组…)和多行条目(带数组…)?
[19-02-2016 16:57:17.104504] [info] system done. 
 0: array(
   'ID' => 'john foo'
 )
[19-02-2016 16:57:17.110482] [info] transaction done. 
   0: array(
      'ID' => 'john foo'
   )
{
"trace": [20]
0:  {
"date": "19-02-2016 16:57:17.104504"
"type": "info"
"message": "system done."
"ID": john foo
}
1:  {
"date": "19-02-2016 16:57:17.110482"
"type": "info"
"message": "transaction done."
"ID": john foo
}
...
<?php
$file = 'test.log';

$content = array(); 
$content["trace"] = array();
$input = file_get_contents('test.log');

preg_match_all('/\[(.*)\][\s]*?\[(.*?)\][\s]*?(.*)[\s][^\']*\'ID\'[ ]*=>[ ]*\'(.*)\'/', $input, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[0]); $i++) {
    $content['trace'][] = array(
        'date'    => $regs[1][$i],
        'type'    => trim($regs[2][$i]),
        'message' => trim($regs[3][$i]),
        'ID'      => trim($regs[4][$i]),
    );
}

// return $content;
echo '<pre>'; print_r($content); echo '</pre>';  // For testing only
$content = json_encode($content);                // For testing only
echo '<pre>' . $content . '</pre>';              // For testing only
Array
(
    [trace] => Array
        (
            [0] => Array
                (
                    [date] => 19-02-2016 16:57:17.104504
                    [type] => info
                    [message] => system done.
                    [ID] => john foo
                )

            [1] => Array
                (
                    [date] => 19-02-2016 16:57:17.110482
                    [type] => info
                    [message] => transaction done.
                    [ID] => john foo
                )

        )

)
{
    "trace":[
        {
            "date":"19-02-2016 16:57:17.104504",
            "type":"info",
            "message":"system done.",
            "ID":"john foo"
        },
        {
            "date":"19-02-2016 16:57:17.110482",
            "type":"info",
            "message":"transaction done.",
            "ID":"john foo"
        }
    ]
}
    \[       Match the character “[” literally
(        Match the regular expression below and capture its match into backreference number 1
    .        Match any single character that is not a line break character
    *        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\]       Match the character “]” literally
[\s]     Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
*?       Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\[       Match the character “[” literally
(        Match the regular expression below and capture its match into backreference number 2
    .        Match any single character that is not a line break character
    *?       Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
\]       Match the character “]” literally
[\s]     Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
*?       Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
(        Match the regular expression below and capture its match into backreference number 3
    .        Match any single character that is not a line break character
    *        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
[\s]     Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
[^']     Match any character that is NOT a “'”
*        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
'ID'     Match the characters “'ID'” literally
[ ]      Match the character “ ”
*        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
=>       Match the characters “=>” literally
[ ]      Match the character “ ”
*        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
'        Match the character “'” literally
(        Match the regular expression below and capture its match into backreference number 4
    .        Match any single character that is not a line break character
    *        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
'        Match the character “'” literally