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,
\
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);