在PHP中格式化JSON格式的文本文件 [英] Formatting JSON formatted text file in PHP

查看:151
本文介绍了在PHP中格式化JSON格式的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我得到了一个带有按钮的HTML页面.当我单击按钮时,一个单独的javascript文件将GET请求发送到我的PHP文件,并期望返回JSON对象.我的PHP读取JSON格式的文本文件,应将其转换为JSONObject并将其回显给我的javascipt.我之前有一些代码在工作,但是由于我改用Ajax方法而不是将所有内容都放在同一个文件中,所以似乎不再这样做了.这是我的代码:

So I got a HTML page with a button. When I click the button, a separate javascript file sends a GET request to my PHP file, expecting a JSON object in return. My PHP reads a JSON formatted text file and should convert it into a JSONObject and echo it out for my javascipt. I had some code working before, but it doesn't seem to do it anymore since I changed to a Ajax aproach instead of having everything in the same file. This is my code:

readLog.php

<?php
class test{

function clean($string){
    return json_decode(rtrim(trim($string),','),true);
}

function getLog(){
    header('Content-Type: application/json');
    $logLines = file('../../../home/shares/flower_hum/humid.log');
    $entries = array_map("clean",$logLines);
    $finalOutput = ['log'  => $entries];
    echo json_encode($logLines);

}
}
 ?>

我的humid.log文件如下所示:

My humid.log file looks like this:

{"date":"26/09/2016", "time":"22:40:46","temp":"16.0", "humidity":"71.0" }     
{"date":"26/09/2016", "time":"23:10:47","temp":"16.0", "humidity":"71.0" }

现在,如果我按我的按钮,这是我在Web浏览器中检查控制台的响应:

Now If I press my button, this is the response I get checking the console in my web browser:

响应:

["{\"date\":\"26\/09\/2016\", \"time\":\"22:40:46\",\"temp\":\"16.0\", \"humidity\":\"71.0\" }{\"date\":\"26\/09\/2016\", \"time\":\"23:10:47\",\"temp\":\"16.0\", \"humidity\":\"71.0\" }\n"]

JSON:

"{"date":"26/09/2016", "time":"22:40:46","temp":"16.0", "humidity":"71.0" }{"date":"26/09/2016", "time":"23:10:47","temp":"16.0", "humidity":"71.0" }\n"

显然格式有问题,但是我不知道是什么.正如我所说,当我将php和HTML放在同一个文件中时,此代码可以正常工作.

obviously something is wrong with the formatting, but I don't know what. As I said, this code worked just fine when I had my php and HTML in the same file.

我也尝试过使用以下格式格式化JSON,但它只显示括号:

I have also tried formatting the JSON with something like this, but it just prints the brackets:

function getLog(){
    $text = file('../../../home/shares/flower_hum/humid.log');
    $textRemoved ="["; //Add opening bracket.
    $textRemoved .= substr($text, 0, strlen($text)-1); (Remove last comma)
    $textRemoved .="]";//Add closing bracket
    $json = json_encode($textRemoved);
    echo $json;
}

推荐答案

您不能仅将两个以上的JSON字符串合并在一起.它是JSON,这意味着它必须在语法上正确的Javascript CODE .如果要将两个json字符串连接在一起,则必须将它们解码为本地数据结构,将这些结构连接在一起,然后重新编码新的合并结构:

You can't just jam two+ JSON strings togther. It's JSON, which means it HAS to be syntactically correct Javascript CODE. If you want to join two json strings together, you HAVE to decode them to a native data structure, join those structures together, then re-encode the new merged structure:

$temp1 = json_decode('{"foo":"bar"}', true);
$temp2 = json_decode('{"baz":"qux"}', true);

$new = array_merge($temp1, $temp2);
echo json_encode($new);

将产生:

{"foo":"bar","baz":"qux"}

并保持有效的JSON/Javascript.

and remain valid JSON/Javascript.

为什么?考虑裸整数是有效的json:

Why? Consider that bare integers are valid json:

json_encode(42) -> 42
json_encode(123) -> 123

如果您有两个json编码的整数并将它们加在一起,则会得到一个新"整数:

If you have two json-encoded integers and jam together, you get a "new" integer:

 42123

,在接收端,您将转到好,那么两者之间的距离在哪里",因为421234212.

and on the receiving end, you'll be going "Ok, so where is the split between the two", because 4 and 2123 are just as valid as possible original values as 4212 and 3.

将两个整数作为不同的值和SEPARATABLE值发送将需要一个数组:

Sending the two integers as distinct and SEPARATABLE values would require an array:

[42,123]

这篇关于在PHP中格式化JSON格式的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆