Saran
Saran

Reputation: 109

Unable to deserialize the list of object data using GSON library

I have a JSON Data which is a collection of objects like the one given below. I have to deserialize the below given data into a list of objects

[{\"Name\": \"Initialize\", \"Library\": \"BKS\", \"Type\": \"setup\", \"Status\": \"PASS\", \"StartTime\": \"20190429 15:06:36.020\", \"EndTime\": \"20190429 15:06:39.476\", \"Environment\": \"CLC-ER\", \"ScenarioName\": \"BKS-DISVAS\", \"ElapsedTime\": 456.0},{\"Name\": \"Initialize\", \"Library\": \"BKS\", \"Type\": \"setup\", \"Status\": \"PASS\", \"StartTime\": \"20190429 15:06:36.020\", \"EndTime\": \"20190429 15:06:39.476\", \"Environment\": \"CLC-ER\", \"ScenarioName\": \"BKS-DISVAS\", \"ElapsedTime\": 456.0}]

I have tried the following code snippet, but I am getting the following error.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at com.google.gson.Gson.fromJson(Gson.java:822) ~[gson-2.3.1.jar:na] at com.google.gson.Gson.fromJson(Gson.java:875) ~[gson-2.3.1.jar:na]

My Model class is given below

public class KeywordDetails {

    public KeywordDetails() {
    }

    @JsonProperty("Name")
    public String Name;
    @JsonProperty("Type")
    public String Type;
    @JsonProperty("Library")
    public String Library;
    @JsonProperty("StartTime")
    public String StartTime;
    @JsonProperty("EndTime")
    public String EndTime;
    @JsonProperty("Status")
    public String Status;
    @JsonProperty("ScenarioName")
    public String ScenarioName;
    @JsonProperty("Environment")
    public String Environment;
    @JsonProperty("ElapsedTime")
    public double ElapsedTime;

}

The deserialization code is given below,

    Gson serializer = new Gson();
    Type listType = new TypeToken<ArrayList<KeywordDetails>>() {
    }.getType();
    JsonParser parser = new JsonParser();
    JsonElement kwJson = parser.parse(ser.getKeywordStats());
    System.out.println(kwJson);

    List<KeywordDetails> keywordDetails = serializer.fromJson(kwJson, listType);

The ser.getKeywordStats() method is giving the above JSON.

I should be able to deserialize the JSON into the list or array of objects.

As I am new to Java, I am not able to find the root cause or fix for this issue, can anyone help me with this issue?

Upvotes: 0

Views: 1555

Answers (4)

Harsh Jatinder
Harsh Jatinder

Reputation: 873

  1. Why JsonProperty instead of SerializableName? Are you mixing Jackson and Gson?
  2. What is the output of ser.getKeywordStats()?

Because I have tested your code by hardcoding that json-string instead of ser.getKeywordStats(), and it worked without any issue.

enter image description here

Upvotes: 3

Mohamed Anees A
Mohamed Anees A

Reputation: 4621

Here is a working snippet,

    private static final String ESCAPED_STRING =
          "[{\"Name\": \"Initialize\", \"Library\": \"BKS\", \"Type\": \"setup\", \"Status\": \"PASS\", \"StartTime\": \"20190429 15:06:36.020\", \"EndTime\": \"20190429 15:06:39.476\", \"Environment\": \"CLC-ER\", \"ScenarioName\": \"BKS-DISVAS\", \"ElapsedTime\": 456.0},{\"Name\": \"Initialize\", \"Library\": \"BKS\", \"Type\": \"setup\", \"Status\": \"PASS\", \"StartTime\": \"20190429 15:06:36.020\", \"EndTime\": \"20190429 15:06:39.476\", \"Environment\": \"CLC-ER\", \"ScenarioName\": \"BKS-DISVAS\", \"ElapsedTime\": 456.0}]";

      public static void main(String[] args) throws JsonProcessingException {
         Gson gson = new Gson();
         Type t= new TypeToken<List<KeywordDetails>>(){}.getType();
         JsonParser parser = new JsonParser();
         JsonElement jsonElements = parser.parse(ESCAPED_STRING);
         System.out.println(jsonElements);
         List<KeywordDetails> kd = gson.fromJson(jsonElements, t);
         System.out.println(kd);
      }

This has to do something with your String, the string must be void of the \ slashes to make it a valid JSON. So, I have used JsonParser to construct a valid JSON from the escaped String.

Upvotes: 2

Agnaramon
Agnaramon

Reputation: 881

I don't know @JsonProperty annotation. You can use (or not) @SerializedName instead.

Then you can simply deserialize your json like this.

String json = ...
Gson gson = new Gson();
List< KeywordDetails > keywords = gson.fromJson(json, new TypeToken<List< KeywordDetails >>(){}.getType());

Upvotes: 1

Jassem Ben Hamida
Jassem Ben Hamida

Reputation: 524

Use Gson or JsonParser not both. Try with this code:

Gson serializer = new Gson();
Type listType = new TypeToken<ArrayList<KeywordDetails>>() {}.getType();
List<KeywordDetails> keywordDetails = serializer.fromJson(ser.getKeywordStats(), listType);

Upvotes: 1

Related Questions