I have a json file and using below code to convert json into java POJO
reader = new JsonReader(new InputStreamReader(responseStream, "UTF-8"));
Gson gson = new GsonBuilder().create();
reader.beginObject();
while (reader.hasNext()) {
Example st = gson.fromJson(reader, Example.class);
}
my json structure is as:
{
"$id": "students.json",
"type": "object",
"properties": {
"project": {
"$id": "project",
"projectList": [
"ABC"
]
},
"students": {
"$id": "/properties/students",
"type": "array",
"subelements": {
"properties": {
"id": {
"$id": "/properties/students/subelements/properties/id",
"examples": [
"Y"
]
},
"dep": {
"$id": "/properties/students/subelements/properties/dep",
"examples": [
"X"
]
}
},
"required": [
"id",
"dep"
]
}
}
},
"required": [
"project"
]
}
And I only need students.subelements.id.examples[0] and students.subelements.dep.examples[0] from list of students currently my java object classes are:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"project",
"elements"
})
public class Example {
/**
* The project
* (Required)
*
*/
@JsonProperty("project")
@JsonPropertyDescription("The project code")
private String project;
@JsonProperty("elements")
private List<Student> elements = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
//student class
public class Student{
private String id;
private String dep;
}
and I am facing below exception:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NAME at line 2 column 4 path $.
so please help me what will be my exact java object class according to provided json and I will get only required fields from that class ?