gpt4 book ai didi

使用 GSON 将 JSON 转为 POJO

转载 作者:行者123 更新时间:2023-12-02 01:59:46 25 4
gpt4 key购买 nike

我正在尝试使用 GSON 将 JSON 对象转换为 POJO。尽管我没有收到任何错误/异常,但包装类中的 List 对象最终仍然为 null。任何想法都是我做错了什么

JSON 字符串

  {
"location":[
{
"id":"1",
"locationName":"Location 1",
"eventType":[
{
"id":"1",
"eventName":"Event 1"
},
{
"id":"2",
"eventName":"Event 2"
},
{
"id":"3",
"eventName":"Event 3"
}
]
},
{
"id":"2",
"locationName":"Location 2",
"eventType":[
{
"id":"4",
"eventName":"Event 4"
},
{
"id":"5",
"eventName":"Event 5"
},
{
"id":"6",
"eventName":"Event 6"
}
]
},
{
"id":"3",
"locationName":"Location 3",
"eventType":[
{
"id":"7",
"eventName":"Event 7"
},
{
"id":"8",
"eventName":"Event 8"
},
{
"id":"9",
"eventName":"Event 9"
}
]
}
]
}

与 GSON 一起使用的 Wrapper 类

public class LocationWrapper {

public List<Location> locationList;

public List<Location> getLocationList() {
return locationList;
}

public void setLocationList(List<Location> locationList) {
this.locationList = locationList;
}


}

位置POJO

public class Location  {

private long id;
private String locationName;
private List<EventType> eventTypeList;

public Location() {

}

public Location(long id, String locationName, ArrayList<EventType> eventTypeList) {
this.id = id;
this.locationName = locationName;
this.eventTypeList = eventTypeList;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getLocationName() {
return locationName;
}

public void setLocationName(String locationName) {
this.locationName = locationName;
}

public List<EventType> getEventTypeList() {
return eventTypeList;
}

public void setEventTypeList(List<EventType> eventTypeList) {
this.eventTypeList = eventTypeList;
}
}

事件类型POJO

public class EventType  {


private long id;
private String eventName;

public EventType() {
}

public EventType(long id, String eventName) {
this.id = id;
this.eventName = eventName;
}

public EventType(int id, String eventName) {
this.id = id;
this.eventName = eventName;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getEventName() {
return eventName;
}

public void setEventName(String eventName) {
this.eventName = eventName;
}
}

我正在使用的方法

private void parseGSONfile(String fileName) {


Gson gson = new Gson();

//getting string from file, you can insert the above string here
String json = new JSONParser().getJSONStringFromFile(fileName);

List<Location> locationList;
LocationWrapper locationWrapper = null;


try {

locationWrapper = gson.fromJson(json, LocationWrapper.class);

} catch (Exception e) {
}

//here the contained object locationList is still null
locationList = locationWrapper.getLocationList();
}

最佳答案

您的 json 为字段 location 提供了一个值,但是您的类 LocationWrapper 有一个名为 locationList 的字段,因此不匹配。重命名字段或使用 @SerializedName

关于使用 GSON 将 JSON 转为 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17835335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com