类型java.lang.String无法在org.json.JSON.typeMismatch(JSON.java:111)转换为JSONObject

时间:2014-12-01 15:42:46

标签: java android json

你可以帮忙吗? 我的PHP Web服务读取发送此JSON:

{
  "version": "1",
  "id": "1",
  "title": "John",
  "text": "Joe",
  "language": "French",
  "image": "ic_launcher1",
  "audio": "au_sound1",
}

我的Android Rest客户端代码返回以下错误:

{
  "version": "1",
  "id": "1",
  "title": "John",
  "text": "Joe",
  "language": "French",
  "image": "ic_launcher1",
  "audio": "au_sound1",
} 
of type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:111)

Android客户端代码如下:

private void webServiceClient (String serviceUrl) {
        String jsonContent;
        InputStream inputStream = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(serviceUrl);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
        }
        catch (Exception e) {
            Log.e("Webservice 1", e.toString());
        }


        try {
            // read the json file ----------------------------------------
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8000);
            StringBuilder sb = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");
            }

            reader.close();
            jsonContent = sb.toString();

            if (jsonContent != null) {
                JSONObject jsonObject = new JSONObject(jsonContent);
                if (jsonObject != null) {
                    JSONArray sectionArray = null;
                    String language = jsonObject.get("language").toString();

                    switch (language) {
                    case "French":
                        sectionArray = (JSONArray) jsonObject.get("section_fr");
                        break;
                    case "English":
                        sectionArray = (JSONArray) jsonObject.get("section_en");
                        break;
                    case "Arabic":
                        sectionArray = (JSONArray) jsonObject.get("section_ar");
                        break;
                    default:
                        break;
                    }

                    List<String> sections = new ArrayList<String>();
                    for (int i = 0; i < sectionArray.length(); i++) {
                        String oneSection = sectionArray.get(i).toString();
                        sections.add(oneSection);
                        Log.d(TAG, "section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
                                + oneSection);
                    }
                    /*
                     * jsonObject.get("id"); jsonObject.get("title");
                     * jsonObject.get("text"); jsonObject.get("language");
                     */
                    addSections(jsonObject.get("id").toString(), jsonObject
                            .get("language").toString(), sections);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

JSON似乎有些不对劲我不知道你的帮助账单会受到非常赞赏。

1 个答案:

答案 0 :(得分:1)

json无效。最后还有一个额外的逗号

 "audio": "au_sound1",
}

应该是

 "audio": "au_sound1"
}

也是chage

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

 jsonContent = EntityUtils.toString(entity)