[英]parse JSON array/object using Gson in java
I want to parse a JSON file in JAVA using GSON and the file format is as follows: 我想使用GSON解析JAVA中的JSON文件,文件格式如下:
{
"parcel":[
{
"firstName": "ABC",
"lastName": "LAST",
"email": "abc.LAST@g.com",
"country": "United States",
"dates": [
"2013-05-01",
"2014-05-07"
]
},
{
"firstName": "CVS",
"lastName": "Bad",
"email": "cvs.Bad@g.com",
"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]. 现在,我得到的只是Parcel [data = null]。
Well the JSON input and your class hierarchy have to be compatible. JSON输入和您的类层次结构必须兼容。 Now you have a class Parcel
with one Data
attribute named "data". 现在,您有了一个具有一个名为“ data”的Data
属性的Parcel
类。 Meanwhile your JSON is an object with an attribute named "class". 同时,您的JSON是具有名为“ class”的属性的对象。 So gson does not find a "class" property in the Parcel class and the "data" attribute get's ignored. 因此,gson在Parcel类中找不到“ class”属性,并且“ data”属性被忽略。
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. 其次,您的JSON具有一个Data数组,而不只是一个Data实例,因此您必须从中创建一个数组。
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. 这可能仍然会崩溃,因为您省略了Data
类中使用的“ Arrays”类。 If you change the type from Arrays
to String
it should work though. 如果您将类型从Arrays
更改为String
它应该可以工作。
Also I used the @SerializedName
annotation to map the data
property to the class
property, since class
is a keyword in java. 我还使用@SerializedName
批注将data
属性映射到class
属性,因为class
是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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.