After hours of search and effort i couldn't fix it.So finally seeking your help.
My Json
[
{
"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
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.
what I have tried
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;
}
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 ;
}
Question
How could i get rid of this issue and get a neat json
which won't result in any jsonexception
?
Note:
I checked carefully many times for linebreaks there.But there are no linebreaks (\n
) etc in my database.
Edited Code
$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 ;
output in browser now
a http://www.4shared.com/download/C4lUKR-1ba/json1.png?lgfp=3000
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!
Weird Solution
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