Reputation: 455
I want to parse a JSON file in JAVA using GSON and the file format is as follows:
{
"parcel":[
{
"firstName": "ABC",
"lastName": "LAST",
"email": "[email protected]",
"country": "United States",
"dates": [
"2013-05-01",
"2014-05-07"
]
},
{
"firstName": "CVS",
"lastName": "Bad",
"email": "[email protected]",
"country": "Iceland",
"dates": [
"2010-04-30",
"2011-01-01",
]
}
]
}
And my main method in the public class goes like this:
public static void main(String[] args) throws IOException {
try(Reader reader = new InputStreamReader(JsonToJava.class.getResourceAsStream("input.json"), "UTF-8")){
Gson gson = new GsonBuilder().create();
Parcel p = gson.fromJson(reader, Parcel.class);
System.out.println(p);
}
}
And the following is the code for Parcel:
public class Parcel {
Data data;
@Override
public String toString() {
return "Parcel [data=" + data + "]";
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
class Data{
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Arrays[] getAvailableDates() {
return dates;
}
public void setAvailableDates(Arrays[] dates) {
this.dates = dates;
}
@Override
public String toString() {
return "Data [firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", country=" + country
+ ", dates=" + Arrays.toString(dates) + "]";
}
private String firstName;
private String lastName;
private String email;
private String country;
private Arrays[] dates;
}
But I can't seem to be hitting the right way to store all values of the variables in a list. Right now All I get is Parcel[data = null].
Upvotes: 1
Views: 561
Reputation: 12537
Well the JSON input and your class hierarchy have to be compatible. Now you have a class Parcel
with one Data
attribute named "data". Meanwhile your JSON is an object with an attribute named "class". So gson does not find a "class" property in the Parcel class and the "data" attribute get's ignored.
Secondly your JSON has an array of Data's instead of just one Data instance, so you'd have to make an array out of it.
So this should work better:
import com.google.gson.annotations.SerializedName;
public class Parcel {
@SerializedName("class")
Data[] data;
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("Parcel [data=");
for(Data singleData : data) {
if (singleData != data[0]) {
result.append(", ");
}
result.append(singleData.toString());
}
result.append("]");
return result.toString();
}
}
This might still crash, since you omitted the "Arrays" class you used in the Data
class. If you change the type from Arrays
to String
it should work though.
Also I used the @SerializedName
annotation to map the data
property to the class
property, since class
is a keyword in java.
Solution with skipping the root Element ("parcel"):
try(Reader reader = new InputStreamReader(new ByteArrayInputStream(jsonString1.getBytes()), "UTF-8")){
Gson gson = new GsonBuilder().create();
JsonElement rootElement = gson.fromJson(reader, JsonObject.class).get("parcel");
Data[] parcels = gson.fromJson(rootElement, Data[].class);
for (Data parcel : parcels) {
System.out.println(parcel);
}
}
Upvotes: 2