JSONException: Value of type java.lang.String cannot be converted to JSONObject

前端 未结 14 2026
别那么骄傲
别那么骄傲 2020-11-22 07:10

I have a JSON file with 2 JSON-Arrays in it: One Array for routes and one Array for sights.

A route should consist of several sights where the user gets navigated to

相关标签:
14条回答
  • 2020-11-22 07:47
    return response;
    

    After that get the response we need to parse this By:

    JSONObject myObj=new JSONObject(response);
    

    On response there is no need for double quotes.

    0 讨论(0)
  • 2020-11-22 07:49

    In my case, my Android app uses Volley to make a POST call with an empty body to an API application hosted on Microsoft Azure.

    The error was:

    JSONException: Value <p>iisnode of type java.lang.String cannot be converted to JSONObject
    

    This is a snippet on how I was constructing the Volley JSON request:

    final JSONObject emptyJsonObject = new JSONObject();
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, emptyJsonObject, listener, errorListener);
    

    I solved my problem by creating the JSONObject with an empty JSON object as follows:

    final JSONObject emptyJsonObject = new JSONObject("{}");
    

    My solution is along the lines to this older answer.

    0 讨论(0)
  • 2020-11-22 07:50

    I think the problem may be in the charset that you are trying to use. It is probably best to use UTF-8 instead of iso-8859-1.

    Also open whatever file is being used for your InputStream and make sure no special characters were accidentally inserted. Sometimes you have to specifically tell your editor to display hidden / special characters.

    0 讨论(0)
  • 2020-11-22 07:52

    I made this change and now it works for me.

    //BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
    
    0 讨论(0)
  • 2020-11-22 07:53

    This worked for me

    json = json.replace("\\\"","'");
    JSONObject jo = new JSONObject(json.substring(1,json.length()-1));
    
    0 讨论(0)
  • 2020-11-22 07:54

    In my case the problem occured from php file. It gave unwanted characters.That is why a json parsing problem occured.

    Then I paste my php code in Notepad++ and select Encode in utf-8 without BOM from Encoding tab and running this code-

    My problem gone away.

    0 讨论(0)
提交回复
热议问题