Value of type java.lang.String cannot be converted to JSONArray

后端 未结 4 1106
情深已故
情深已故 2020-12-02 02:55

I\'ve spent 2 days to find a solution with of problem.

Here is the error:

E/log_tag: Error parsing data org.json.JSONException: Value of type java.l         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 03:37

    The problem is that your JSON is in the incorrect format. I have tried with your sample JSON and found the solution to it. Now the inbuilt JSONObject and JSONArray cannot be used to get such a json response.

    You need to add json-simple library to your project by adding it to gradle:

    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    

    Or download the library "json-simple-1.1.1.jar" from this link https://repo1.maven.org/maven2/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar

    Then you can parse your JSON easily and it won't give any error. I have made a small sample code for you on how to use it :

    import org.json.simple.JSONArray;
    import org.json.simple.parser.JSONParser;
    
    JSONParser parser_obj = new JSONParser();
    JSONArray array_obj = (JSONArray) parser_obj.parse("String from web service"); 
    // in your case it will be "result" 
    

    Then you can process it as per your need.

提交回复
热议问题