GSON throwing “Expected Expected a name but was NUMBER at line 1 column 8”?

后端 未结 3 1685
心在旅途
心在旅途 2021-01-11 23:11

I\'m trying to parse a JSON string like this one ( generated URL with http://www.json-generator.com)

{
\"total\": 86, 
\"jsonrpc\": \"2.0\", 
\"id\": 1, 
\         


        
3条回答
  •  生来不讨喜
    2021-01-11 23:55

    If you're going to use JsonReader and parse the stream manually, you don't use the automatic deserialization via Gson.fromJson().

    You either need to extract the fields from each object while traversing that array once you're in your loop, or simply use the automatic deserialization with an appropriate class (which, honestly, is what you should be doing):

    class Response {
        private int total;
        private String jsonrpc;
        private int id;
        private List result;
    
       // getters and setters ...
    }
    

    And then simply:

    InputStreamReader isr = new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
    Response r = gson.fromJson(isr, Response.class);
    

提交回复
热议问题