org.json.JSONException:类型为java.lang.String的值不能转换为JSONObject [英] org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

查看:4923
本文介绍了org.json.JSONException:类型为java.lang.String的值不能转换为JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看到其他主题,他们正在谈论的是编码(服务器)或其他的东西服务器端,但我试图把所有我的JSON字段中的测试一个问题仍然存在。



这是我的代码:

  URL url = new URL(c.getString(R.string.url_ws)+ url_ws); 
HttpURLConnection connection =(HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty(Content-Type,application / x-www-form-urlencoded);
connection.setRequestMethod(POST);
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream());
request.write(& test = test);
request.flush();
request.close();


字符串;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

while((line = reader.readLine())!= null){
sb.append(line +\\\
);
}
String json = sb.toString()。trim();
try {
JSONObject obj = new JSONObject(json);
} catch(JSONException e){
e.printStackTrace();
Log.d(Tools.TAG +/ debug JSONException,e.toString());
返回null;
}

这是我的例外



调用JSONException(4184):org.json.JSONException:类型为java.lang.String的值不能转换为JSONObject

我看到Value和of之间的空格,所以我猜问题是一个空字符串,但我没有得到它。



谢谢。



编辑



我的JSON中的编码错误,

  char a = json.substring(0,1).charAt(0); 
int ascii =(int)a;
Tools.myLog(>+ ascii +<);

我发现65279 char



解决方案



为什么& #65279;出现在我的HTML?



编码没有BOM的UTF-8。

解决方案

  while((line = reader.readLine())!= null){
sb.append(line +\\\
);
}

而不是尝试

  while((line = reader.readLine())!= null){
sb.append(line);
json = sb.toString()。substring(0,sb.toString()。length() - 1);
}

按照此方法获取json对象:

  public JSONObject getJSONObjFromUrl(String url,List< NameValuePair> params){
System.out.println(url ::+ url);
System.out.println(params ::+ params ++ params.get(0));
//进行HTTP请求
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch(UnsupportedEncodingException e){
e.printStackTrace();
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is,iso-8859-1),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!= null){
sb.append(line);
}
is.close();

json = sb.toString()。substring(0,sb.toString()。length() - 1);
// Log.e(JSON ::,json);
} catch(Exception e){
Log.e(缓冲区错误,转换结果错误+ e.toString());
}

//尝试将字符串解析为JSON对象
try {
jObj = new JSONObject(json);
} catch(JSONException e){
Log.e(JSON Parser,解析数据错误+ e.toString());
}

//返回JSON String

return jObj;

}


Hi i got a problem with Android JSON Parser in 2.3, all is ok in 4.0>.

I look other topic they are talking about encoding (server) or other stuff server side, but i tried to put "test" in all my JSON field an the problem still persist.

Here is my code :

URL url = new URL(c.getString(R.string.url_ws) + url_ws);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream());
request.write("&test=test");
request.flush();
request.close();


String line;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}
String json = sb.toString().trim();
try {
     JSONObject obj = new JSONObject(json);
} catch (JSONException e) {
    e.printStackTrace();
    Log.d(Tools.TAG+"/debug JSONException", e.toString());
    return null;
}

Here is my exception

debug JSONException(4184): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

i saw double space between "Value" and "of" so i guess the problem is a empty string but i dont get it.

thanks.

EDIT

it was a encode mistake in my JSON,

char a = json.substring(0, 1).charAt(0);
int ascii = (int)a;
Tools.myLog(">"+ascii+"<"); 

i found 65279 char

SOLUTION

Why is &#65279; appearing in my HTML?

encoding UTF-8 without BOM.

解决方案

while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}

instead try

while ((line = reader.readLine()) != null){
    sb.append(line);
   json = sb.toString().substring(0, sb.toString().length()-1);
}

Follow this method for getting json object:

 public JSONObject getJSONObjFromUrl(String url, List<NameValuePair> params) {      
            System.out.println("url:: "+url );
            System.out.println("params:: "+ params +" " +params.get(0) );
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                is.close();

                json = sb.toString().substring(0, sb.toString().length()-1);
               // Log.e("JSON:: ", json);
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }

            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            // return JSON String

            return jObj;

        }

这篇关于org.json.JSONException:类型为java.lang.String的值不能转换为JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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