How can I parse errors using GSON?

how to use gson
gson annotations
gson maven
gson documentation
gson parse json
gson api
gson deserialize
import gson

My response is:

{
    "data":[],
    "meta":[],
    "errors":[["invalid credentials"]]
}   

I am not able to de-serialize it by below method:

Errors errors = gson.fromJson(response, new TypeToken<Errors>(){}.getType());

Errors

public class Errors {
    private HashMap<String, Object> errors;

    public HashMap<String, Object> getErrors() {
        return errors;
    }
}

How to de-serialize errors correctly?

In your example Errors is not Hashmap, it is an array of arrays. Change the class to this and see whether it works or not.

public class Errors {

  private List<List<String>> errors;

  public List<List<String>> getErrors() {
     return errors;
  }

}

Gson User Guide - gson, It can also be used to convert a JSON string to an equivalent Java object. Whether you have to convert JSON to Java Object or otherwise, you need to write a lot of code that is tightly coupled with the JSON structure. Table of Contents [ hide] 1 Gson. 1.1 Gson Maven. 1.2 Gson Example. 1.3 Gson Example Parse JSON using Streaming API. 1.4 Gson Example to Write Object to File.

Try this code

    Errors errors=new Gson().fromJson("here your error data",Errors.class);

Error Deserializing objects using fromJson(string, type) · Issue #140 , gson.JsonParseException: Failed parsing JSON source: java.io.StringReader@​5b205b20 to Json at com.google.gson.JsonParser.parse(  Gson JsonParser is used to parse Json data into a parse tree of JsonElement and thus JsonObject. JsonObject can be used to get access to the values using corresponding keys in JSON string. 1. Create JsonParser. JsonParser class has only one default constructor and it does not require any argument or configuration. JsonParser parser = new JsonParser(); 2.

You are de-serializing - not errors - but a response having these errors. There JSON presents an object say Response that contains data, meta & errors.

So you cannot not pass the JSON in your question and expect it to be de-serialized as an instance of Errors. Instead you need some DTO that matches to your JSON response string. It could be like:

@Getter @Setter
public class Response {
    // You can comment/remove data & meta
    // if you do not need then
    private List<?> data;
    private List<?> meta;
    private List<List<String>> errors;
}

De-serialize like:

Response res = new Gson().fromJson(YOUR_JSON, Response.class);

and get errors like:

res.getErrors();

google/gson: A Java serialization/deserialization library to , Gson is a Java library that can be used to convert Java Objects into their JSON useful for error handling Opened by GoogleCodeExporter about 5 years ago  GSON also has two other parsers. The Gson JSON parser which can parse JSON into Java objects, and the JsonReader which can parse a JSON string or stream into tokens (a pull parser). This tutorial focuses on the JsonParser though - GSON's tree parser. Creating a JsonParser. Before you can use the GSON JsonParser you must create a JsonParser

com.google.gson.JsonParser.<init> java code examples, Best Java code snippets using com.google.gson. Gson gson = new Gson(); JsonParser parser=new JsonParser(); //object arr example getAsInt(); } } catch (​Exception ex) { logger.warn("Error parsing exposed port", ex); } return -1; }. Gson provide simple toJson() and fromJson() methods to convert Java objects to / from JSON.. toJson() – Java object to JSON Gson gson = new Gson(); // 1. Java object to JSON file gson.toJson(obj, new FileWriter("C:\\fileName.json")); // 2.

Gson - How to parse JSON, 3.1 In Gson, we can use gson.toJson() to convert Java objects to JSON. GsonExample1.java. package com.mkyong;  This is the main class for using Gson. Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. com.google.gson.JsonParser: A parser to parse Json into a parse tree of JsonElements; com.google.gson.stream.JsonReader: Reads a JSON encoded value as a stream of tokens.

Gson - Quick Guide, Learn to convert object to json; and convert json to java object. Learn to pretty print json. Learn to read and parse JSON with Google Gson library. Love computers, programming and solving everyday problems. Find me on Facebook and  you know worst thing is nobody upvote or follow. Gson uses reflection so it does not require additional modifications to classes of (de)serialized objects. In fact it just needs the class to have defined default no-args constructor (not entirely t

Comments
  • I tried your code but I got "System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException"
  • that time your json is wrong format check into jsonviewer website.