如何将带有特殊字符的字符串转换为json对象

时间:2011-07-28 06:56:28

标签: android

您好我正在尝试解析字符串并将其转换为JSONObject。但它没有转换并给出错误,如

"07-28 11:36:47.674: WARN/System.err(6050): org.json.JSONException: Unterminated string at character 3136 of {"data":[{""...."(there is my data)

首先我认为有一些特殊字符,例如~,@,%,&,并用" "替换字符,但没有结果并给出相同的错误。

我通过UTF-8格式编码修改了Web服务数据。我使用代码来解码我的应用程序中的UTF-8格式化数据。这是我的代码:

                           HttpParams params = new BasicHttpParams();
               HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
               HttpProtocolParams.setContentCharset(params, "UTF-8");
           params.setBooleanParameter("http.protocol.expect-continue",false);
               HttpClient httpclient = new DefaultHttpClient(params);
               HttpGet httpget =new htpGet("http://www.mylink.com"+todaydate);


                   try
                   {
                        HttpResponse response = httpclient.execute(httpget);
                        HttpEntity entity = response.getEntity();
                        jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
                        Log.d("TEST", jsonText);   /this is the result string

                              }
                   catch (Exception e)
                   {
                   }

 JSONObject jObj = new JSONObject(jsonText.toString()); 

此处我无法将字符串(jsonText)转换为json object(jobj)。但在 logcat 中,它完全显示字符串jsonText。是否有任何建议将数据作为json对象。

我需要在我的应用程序中使用Json object

1 个答案:

答案 0 :(得分:0)

如果您确定JSON数据有效并且没有特殊字符导致问题,那么“字符未终止字符串”可能意味着您没有收到整个字符串。

您可以通过检查结尾的子字符串并在logcat中显示它来证明这一点

如果没有,那么你需要再次开始寻找特殊字符

相关问题