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

前端 未结 14 2045
别那么骄傲
别那么骄傲 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:57

    Had the same problem for few days. Found a solution at last. The PHP server returned some unseen characters which you could not see in the LOG or in System.out.

    So the solution was that i tried to substring my json String one by one and when i came to substring(3) the error went away.

    BTW. i used UTF-8 encoding on both sides. PHP side: header('Content-type=application/json; charset=utf-8');

    JAVA side: BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);

    So try the solution one by one 1,2,3,4...! Hope it helps you guys!

    try {
                jObj = new JSONObject(json.substring(3));
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
            }
    
    0 讨论(0)
  • 2020-11-22 07:58

    if value of the Key is coming as String and you want to convert it to JSONObject,

    First take your key.value into a String variable like

     String data = yourResponse.yourKey;
    

    then convert into JSONArray

    JSONObject myObj=new JSONObject(data);
    
    0 讨论(0)
提交回复
热议问题