Android:错误将字符串转换为JSONObject

时间:2014-04-28 18:23:16

标签: android mysql json

我正在尝试将数据作为JSON从我的数据库中检索回来,并将结果设置为JSONObject,然后在文本视图中显示。

以下是代码:

private class ShowDBActivity3 extends AsyncTask<Void, Void, String>{

    protected String doInBackground(Void... voids){
        String s = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("username", username));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("URL");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }


        try {
            Log.e("This is the result: ", result.toString());
            JSONObject jArray = new JSONObject(result);
            String title = jArray.getString("title");
            String rating = jArray.getString("rating");
            String username = jArray.getString("username");

            s = "Movie: " + title + "\n" +
                "Rating: " + rating + "\n";

            return s;


        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        }
        return s;
    }

    protected void onPostExecute(String s){
        show.setText(s);
    }
}

以下是返回的结果:

这是结果:: [{“0”:“16”,“id”:“16”,“1”:“绿巨人”,“标题”:“绿巨人”,“2”:“ 7" , “等级”: “7”, “3”: “tlong3”, “用户名”: “tlong3”},{ “0”: “19”, “ID”: “19”, “1”:” The Incredible Hulk“,”title“:”The Incredible Hulk“,”2“:”3“,”rating“:”3“,”3“:”tlong3“,”username“:”tlong3“},{” 0 “:” 20" , “ID”: “20”, “1”: “Testtt”, “标题”: “Testtt”, “2”: “6”, “等级”: “6”, “3” : “Tlong3”, “用户名”: “Tlong3”}]

...由于某种原因无法转换为JSONObject。这是错误:

04-28 13:14:25.372 20099-20830 / com.android.movi​​es E / Fail 3:org.json.JSONException:Value [{“3”:“tlong3”,“id”:“16”, “2”:“7”,“用户名”:“tlong3”,“标题”:“绿巨人”,“1”:“绿巨人”,“0”:“16”,“评级”:“7”} ,{“3”:“tlong3”,“id”:“19”,“2”:“3”,“username”:“tlong3”,“title”:“The Incredible Hulk”,“1”:“The难以置信的绿巨人“,”0“:”19“,”评级“:”3“},{”3“:”Tlong3“,”id“:”20“,”2“:”6“,”用户名“: “Tlong3”,“title”:“Testtt”,“1”:“Testtt”,“0”:“20”,“rating”:“6”}]类型org.json.JSONArray无法转换为JSONObject < / p>

1 个答案:

答案 0 :(得分:0)

它很安静,logcat也显示JSONArray无法转换为JSONObject。您收到的字符串是JSONArray而不是对象,您试图将其解析为导致错误的JSONObjct。

如果您还不知道,这是给您的。 JSONArray可以由[ Your JSONArray ]标识,JSONObject可以由{ Your JSONObject }标识

您可以使用this工具验证JSON响应并确定其格式。根据你JSONresponse它看起来像[ {***} {***} {***} ]这意味着你有一个JSONArray的JSONObjects来正确解析它你需要先将它转换为JSONArray然后申请循环来遍历它的每个对象。 我已经回答了类似问题herehere,您应该检查一下并尝试类似的问题。

PS:如果您正在开发服务器脚本,那么我建议您对服务器端进行编码,使您的响应始终是JSONObject而不是JSONArray,但这只是在您客户端和服务器端的开发人员。如果您需要帮助,请向我提问。