从php json输出中删除换行符 [英] Removing linebreak from php json output

查看:95
本文介绍了从php json输出中删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的搜索和努力,我无法解决它.所以最终寻求您的帮助.

After hours of search and effort i couldn't fix it.So finally seeking your help.

[
{
    "notice_id": "2",
    "n_header": "Class Test",
    "n_subject": "Class Test from 15-jan",
    "n_datetime": "2014-01-05 09:00:00",
    "noticenum": "NISTA1",
    "n_body": "Dear Students Class test 1 will be held from january 15. \nDetaled Notice will be notified further with timetable",
    "n_removeby": "2014-01-05",
    "n_givenby": "7",
    "nconcerned_id": "1",
    "nconcerned_batch": "2010",
    "nconcerned_degree": "BTECH",
    "nconcerned_section": " "
},
{
    "notice_id": "3",
    "n_header": "Comprehensive Viva",
    "n_subject": "Comprehensive viva from 20-feb",
    "n_datetime": "2014-02-05 10:00:00",
    "noticenum": "NISTB1",
    "n_body": "Students under me for comprehensive\n viva are hereby informed to clear their viva before 20th feb. After 20 feb no viva would be entertained under me.",
    "n_removeby": "2014-02-21",
    "n_givenby": "1",
    "nconcerned_id": "4",
    "nconcerned_batch": "2010",
    "nconcerned_degree": "BTECH",
    "nconcerned_section": "IT"
}
]

但是,当我在浏览器中看到它时,它看起来像:

However when i see it in my browser it looks like :

a http://www.4shared.com/download/D1UQsmEbce/json.png?lgfp = 3000

如您所见,它会不合要求地破裂.结果,当我在android app中将它parse损坏时,我会得到一个异常value <br of type java.lang.String can't be converted to JSONArray,我相信是因为这个linebreak问题引起的.

As you see it breaks up undesirably.As a result when i parse it in my android app i get an exception value <br of type java.lang.String can't be converted to JSONArray which i believe because of this linebreak issue only.

我尝试了许多操作,包括preg_replacestr_replace等,以逃避\r\n or <br>等,但无法为我工作.最后,我有一个像这样使用的功能:

I tried many things including preg_replace,str_replace etc in order to escape \r\n or <br> etc but couldn't get it work for me.Finally i have a function which i am using like this :

function parse($text) {

$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);
$text = trim( preg_replace( '/\s+/', '<br/>', $text));
// JSON requires new line characters be escaped
 $text = str_replace("\n", "\\n", $text);
return $text;

}

我正在编写查询以从postgresql数据库中检索数据.因此,运行以下循环.

I am writing a query to retrieve data from postgresql database.Hence running the following loop.

for ($i=0; $i<$rows; $i++)
{

$row = pg_fetch_array($result, $i, PGSQL_ASSOC);

$json['notice_id'] = trim(strip_tags($row['notice_id']));
$json['n_header'] = trim(strip_tags($row['n_header']));
$json['n_subject'] = trim(strip_tags($row['n_subject']));
$json['n_datetime'] = trim(strip_tags($row['n_datetime']));
$json['noticenum'] = trim(strip_tags($row['noticenum']));
$json['n_body'] = trim(strip_tags($row['n_body']));
$json['n_removeby']=  trim(strip_tags($row['n_removeby']));
$json['n_givenby'] = trim(strip_tags($row['n_givenby']));
$json['nconcerned_id'] = trim(strip_tags($row['nconcerned_id']));
$json['notice_id'] = trim(strip_tags($row['notice_id']));
$json['nconcerned_batch'] = trim(strip_tags($row['nconcerned_batch']));
$json['nconcerned_degree'] = trim(strip_tags($row['nconcerned_degree']));
$json['nconcerned_section'] = trim(strip_tags($row['nconcerned_section']));

parse($json['notice_id']); 
parse($json['n_header']); 
parse($json['n_subject']); 
parse($json['n_datetime']); 
parse($json['noticenum']); 
parse($json['n_removeby']); 
parse($json['n_givenby']); 
parse($json['nconcerned_id']); 
parse($json['notice_id']); 
parse($json['nconcerned_batch']); 
parse($json['nconcerned_degree']); 
parse($json['nconcerned_section']); 


$data[] = $json;


}

$h = json_encode($data);

echo $h ;


}

问题

我如何摆脱这个问题,得到一个整洁的json,不会导致任何jsonexception?

Question

How could i get rid of this issue and get a neat json which won't result in any jsonexception?

我仔细检查了很多换行符,但是数据库中没有换行符(\n)等.

I checked carefully many times for linebreaks there.But there are no linebreaks (\n) etc in my database.

修改后的代码

$data = array ();
for ($i=0; $i<$rows; $i++)
{

$row = pg_fetch_array($result, $i, PGSQL_ASSOC);

$json['notice_id'] = $row['notice_id'];
$json['n_header'] = $row['n_header'];
$json['n_subject'] = $row['n_subject'];
$json['n_datetime'] = $row['n_datetime'];
$json['noticenum'] = $row['noticenum'];
$json['n_body'] = $row['n_body'];
$json['n_removeby']=  $row['n_removeby'];
$json['n_givenby'] = $row['n_givenby'];
$json['nconcerned_id'] = $row['nconcerned_id'];
$json['notice_id'] = $row['notice_id'];
$json['nconcerned_batch'] = $row['nconcerned_batch'];
$json['nconcerned_degree'] = $row['nconcerned_degree'];
$json['nconcerned_section'] = $row['nconcerned_section'];


$json['notice_id']=parse($json['notice_id']); 
$json['n_header']=parse($json['n_header']); 
$json['n_subject']= parse($json['n_subject']); 
$json['n_datetime']=parse($json['n_datetime']); 
$json['noticenum']=parse($json['noticenum']); 
$json['n_removeby']=parse($json['n_removeby']); 
$json['n_givenby']=parse($json['n_givenby']); 
$json['nconcerned_id']=parse($json['nconcerned_id']); 
$json['notice_id']=parse($json['notice_id']); 
$json['nconcerned_batch']=parse($json['nconcerned_batch']); 
$json['nconcerned_degree']=parse($json['nconcerned_degree']); 
$json['nconcerned_section']=parse($json['nconcerned_section']); 


$data[] = $json;

}

$h = json_encode($data);

echo $h ;

立即在浏览器中输出

a http://www.4shared.com/下载/C4lUKR-1ba/json1.png?lgfp=3000

这是另一个在我的浏览器中整齐显示的json.

Here is another json which shows up neatly in my browser.

a http://www.4shared.com/download/tNWhDbfuce/ajson.png?lgfp = 3000

奇怪的是为什么另一个人没有换行!

It's strange why the other one not having a linebreak!

奇怪的解决方案

我不确定是什么解决了这个问题.但是当我更改用于执行我的php文件的url并为我工作时,请参考

I am not sure what solved this problem.But when i changed the url which i was using to execute my php file and it worked for me.Please refer here

推荐答案

您误解了浏览器显示的内容.请记住,JSON本质上是纯文本,但是您的浏览器正在尝试将其显示为HTML. HTML模式显示不支持\n字符,它们会将文本包装在第一个适当的空格字符处. JSON可以很好地将\n字符保留在其字符串中而没有任何麻烦.

You're misinterpreting what your browser is displaying. Remember that JSON is essentially plain text, but your browser is trying to display it as HTML. \n chars are NOT honored by HTML-mode displays, and they will wrap the text at the first appropriate space character. JSON can perfectly well keep \n chars inside its strings without any trouble.

您的<br>错误很可能是由于您在preg_replace调用中所做的<br>插入而引起的,因为原始JSON中没有<br>标记.换句话说,通过尝试修复如果不尝试修复该错误将不存在的错误,将导致正试图修复的错误.

Most likely your <br> error is coming from the <br> insertion you're doing in your preg_replace call, because there are NO <br> tags in the original JSON. In other words, you're causing the very error you're trying to fix, by trying to fix the error which wouldn't exist if you weren't trying to fix it.

这篇关于从php json输出中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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