从php json输出中删除换行符

从php json输出中删除换行符,php,json,postgresql,Php,Json,Postgresql,经过数小时的搜索和努力,我无法修复它。因此,我终于寻求您的帮助 我的Json 但是,当我在浏览器中看到它时,它看起来像: 正如你所看到的,它不受欢迎地崩溃了。因此,当我在android应用程序中解析它时,我得到了一个异常值你误解了浏览器显示的内容。请记住,JSON本质上是纯文本,但您的浏览器试图将其显示为HTML\HTML模式显示不支持n个字符,它们将在第一个适当的空格字符处包装文本。JSON可以很好地将\n字符保留在其字符串中,而不会出现任何问题 您的错误很可能来自您在preg_replace

经过数小时的搜索和努力,我无法修复它。因此,我终于寻求您的帮助

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


正如你所看到的,它不受欢迎地崩溃了。因此,当我在android应用程序中解析它时,我得到了一个异常值你误解了浏览器显示的内容。请记住,JSON本质上是纯文本,但您的浏览器试图将其显示为HTML\HTML模式显示不支持n个字符,它们将在第一个适当的空格字符处包装文本。JSON可以很好地将\n字符保留在其字符串中,而不会出现任何问题


您的错误很可能来自您在preg_replace调用中所做的插入,因为原始JSON中没有标记。换言之,您正试图修复一个错误,而如果您不尝试修复该错误,该错误将不存在。

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


您的错误很可能来自您在preg_replace调用中所做的插入,因为原始JSON中没有标记。换句话说,您正试图修复一个错误,而如果您不尝试修复它,这个错误将不存在。根据您的输入,我完成了工作示例,具体取决于堆栈溢出中的一些答案

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代码,我认为问题来自他们的代码,希望这对您有所帮助。

好的,根据您的输入,我完成了工作示例,具体取决于stack overflow的一些回答

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代码,我相信这是他们的问题,希望这对你有帮助。

你只需在解码前添加新行,它将100%工作:

$text = str_replace("\r\n", "\n", $text);

您只需在解码前添加新行,它将100%工作:

$text = str_replace("\r\n", "\n", $text);

作为后续操作,尝试在HTML输出之前添加以下PHP代码:headerContent类型:application/json;字符集=UTF-8;我在我的浏览器中从来没有看到过这种json换行符。其他具有不同查询的php文件以整洁的方式显示。因此我假设了我所提到的。即使我注释掉解析函数,我在android应用程序中也会得到相同的json输出和相同的异常。作为后续,尝试在HTML输出之前添加此PHP代码:headerContent类型:application/json;字符集=UTF-8;在我的浏览器中,我从来没有看到过json的这种换行符。其他具有不同查询的php文件以整洁的方式显示。因此我假设了我所提到的。即使我注释掉解析函数,我在android应用程序中也会得到相同的json输出和相同的异常。好的。请检查我的编辑。我如上所述分配了变量。这样可以吗分配相同的变量或不同的变量是必需的?可以这样做,结果是什么?请检查我的编辑。发生了一个非常轻微的变化。但该值也检查我附加的另一张图片。无法理解为什么另一张图片有换行符!如果不使用AsynTask,现在使用字符串url=http://192.168.32.1/Aafois/notice.php?isBatch=2010§ion1=“它”;但当我使用字符串URL=http://192.168.32.1/Aafois/notice.php?isBatch=+isbatch+§ion1=+'+section1+';我得到了JSONException.isbatch和section1是两个字符串变量,它们也是URL编码的。好的。请检查我的编辑。我如上所述分配了变量。可以分配相同的变量还是需要不同的变量?可以这样做,你得到的结果是什么?请检查我的编辑。发生了一个非常轻微的更改。但是该值也检查了我附加的另一张图片。无法理解为什么另一张图片有换行符!如果不使用AsynTask,现在使用字符串url=http://192.168.32.1/Aafois/notice.php?isBatch=2010§ion1=“它”;但当我使用字符串URL=http://192.168.32.1/Aafois/notice.php?isBatch=+isbatch+§ion1=+'+section1+';我得到了JSONException.isbatch和section1是两个字符串变量,它们也是URL编码的。
<?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);
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>
MyAsyncTask task = new MyAsyncTask();
        task.execute();
$text = str_replace("\r\n", "\n", $text);