经过几个小时的搜索和努力,我无法解决它。所以最后寻求你的帮助。
[
{
"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"
}
]
然而,当我在浏览器中看到它时,它看起来像:
a http://www.4shared.com/download/D1UQsmEbce/json.png?lgfp=3000
正如你所看到的那样,它不合时宜地被破坏了。结果当我parse
我的android app
我得到了一个异常value <br of type java.lang.String can't be converted to JSONArray
我认为这是因为linebreak
这个问题仅
我尝试了许多内容,包括preg_replace
,str_replace
等以逃避\r\n or <br>
等但无法让它为我工作。最后我有一个我正在使用的功能这个:
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
database检索数据。运行以下循环。
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
?
我仔细检查过那里的换行符。但我的数据库中没有换行符(\n
)等。
已编辑的代码
$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/download/C4lUKR-1ba/json1.png?lgfp=3000
这是另一个json,它在我的浏览器中整齐地显示出来。
a http://www.4shared.com/download/tNWhDbfuce/ajson.png?lgfp=3000
奇怪为什么另一个人没有换行!
奇怪的解决方案
我不确定是什么解决了这个问题。但是当我更改了url
我正在使用的php
来执行我的{{1}}文件并且它对我有用时。请参考here
答案 0 :(得分:4)
您误解了浏览器的显示内容。请记住,JSON本质上是纯文本,但您的浏览器正在尝试将其显示为HTML。 \n
字符不受HTML模式显示的支持,它们将文本包装在第一个适当的空格字符处。 JSON可以毫不费力地将\n
字符保留在字符串中。
您的<br>
错误很可能来自您<br>
电话中的preg_replace
插件,因为原始JSON中没有<br>
个标签。换句话说,通过尝试修复错误,如果您没有尝试修复错误,则会导致您尝试修复的错误。
答案 1 :(得分:1)
根据您的输入确定我完成了工作示例,具体取决于堆栈溢出时的一些答案,Parse JSON from URLon android。
PHP部分
我有你的数据,所以我做了这样的例子
<?php
$row = array (
"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" => " ");
$row2 = array ("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");
$data [] =$row;
$data [] = $row2;
echo json_encode ($data);
Android部分
class MyAsyncTask extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
InputStream inputStream = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Downloading your data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
MyAsyncTask.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
String url_select = "http://192.168.10.206/test.php";
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
// Set up HTTP post
// HttpClient is more then less deprecated. Need to change to URLConnection
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// Read content & Log
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
// Convert response to string using String Builder
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
}
return null;
} // protected Void doInBackground(String... params)
protected void onPostExecute(Void v) {
//parse JSON data
try {
JSONArray jArray = new JSONArray(result);
for(int i=0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String name = jObject.getString("n_body");
String tab1_text = jObject.getString("n_removeby");
int active = jObject.getInt("notice_id");
Log.i("NAME",name);
Log.i("REMOVE",tab1_text);
} // End Loop
this.progressDialog.dismiss();
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
} // protected void onPostExecute(Void v)
} //class MyAsyncTask extends AsyncTask<String, String, Void>
不要忘记在某个地方调用MyAsync任务。
MyAsyncTask task = new MyAsyncTask();
task.execute();
这个工作非常好,所以请查看你的安卓代码,我相信问题来自他们,希望这对你有帮助。
答案 2 :(得分:0)
您只需在解码之前添加新行,它将可以100%工作:
$text = str_replace("\r\n", "\n", $text);