JSON解析问题

我想在我的Android应用程序中解析Json解析链接是https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776

当我把它放入Json对象时它会给我带来错误错误是:08-31 14:40:52.281:WARN / System.err(416):org.json.JSONException:java.lang.String类型的值无法转换到JSONObject

public static String getmyproductquestiondetails(String userid, String campaignid) {// https://www.buzzador.com/apps/present_software/webservice/index.php?op=EducationResult&userid=1&questionid=1,2,3&answergivenbyuser=1,1,0 String data = null; try { URL url = new URL( "http://dignizant.com/buzz/webservice/index.php?op=getProductQuestion&userid=" + userid + "&campaign_id=" + campaignid); if (url.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) url .openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); http = https; } else { http = (HttpURLConnection) url.openConnection(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Utils utils = new Utils(); try { data = utils.convertStreamToString(http.getInputStream()); System.out.println("getproduct details response :: " + data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); data = e.toString(); } return data; } try { JSONObject jo = new JSONObject(response); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } 

  char[] utf8 = null; StringBuilder properString = new StringBuilder(""); utf8 = Response.toCharArray(); for (int i = 0; i < utf8.length; i++) { if ((int) utf8[i] < 65000) { properString.append(utf8[i]); } } System.out.println("Response of Login::" + properString.toString()); 

有类似的问题。 起初我的应用程序在androids 4.0+和4.0-(2.3.3 2.2等)上都运行良好。 修改后,我有这个问题。 JSonarray可以在2.3.3上解析

问题:Json STRING(来自服务器的响应)前面带有一个字符,所以实际响应= [{“1”:“omg”}]而不是正确的一个[{“1”:“omg”}]

解决方案:如果字符串dosent以[然后编辑响应字符串(删除’字符)开头

  if (result.startsWith("[")) { } else { result= result.substring(1); } 

之后一切都对我很好

如果您使用json-lib-2.4作为库,我假设您可以解析字符串:

 JSONSerializer.toJSON(yourString).toString() 

而不是使用JsonObject类

要删除json字符串中的字符,如(\ n)或不需要的字符,请在程序中使用commons-lang3:3.4库。 我用这个类来删除json字符串“StringEscapeUtils.unescapeJava(string)”中不需要的字符。

这会对你有所帮助。