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?