JSON parsing issue

I am trying to parse Json in my Android application, link https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776

when I put it in a Json object it gives me errors error: 08-31 14: 40: 52.281: WARN / System.err (416): org.json.JSONException: value like java.lang.String cannot be converted to 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(); } 
+3
source share
4 answers
  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()); 
+10
source

There was a similar problem. At first, my application worked perfectly with both Android 4.0+ and 4.0- (2.3.3 2.2, etc.). after review I have this problem. JSonarray could parse on 2.3.3

PROBLEM: Json STRING (response from the server) has a β€œin front” character so the actual answer = ' [{"1": "omg"}] and not the correct one [{"1": "omg"}]

Solution: if the dosent line starts with [then edit the response line (delete the character)

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

after that everything worked fine for me

+4
source

If you use the json-lib-2.4 library as, I suppose you can parse strings with:

 JSONSerializer.toJSON(yourString).toString() 

instead of using the JsonObject class

+1
source

To remove a character like (\ n) or an unnecessary character in a json string, use the commons-lang3: 3.4 library in the program. I used this class to remove an unwanted character in the json string "StringEscapeUtils.unescapeJava (string)".

it will help you.

0
source

Source: https://habr.com/ru/post/946668/


All Articles