gpt4 book ai didi

java - 如何使用Java将Json字符串数组设置为Java对象

转载 作者:行者123 更新时间:2023-12-02 13:09:19 24 4
gpt4 key购买 nike

我有以下 JSON 字符串,需要将其设置为 POJO 类的 Java 对象。我应该遵循什么方法?

 {"status":"FOUND","messages":null,"sharedLists":      [{"listId":"391647d","listName":"/???","numberOfItems":0,"colla   borative":false,"displaySettings":true}] }

我尝试使用 Gson,但它对我不起作用。

Gson gson = new Gson();
SharedLists target = gson.fromJson(sb.toString(), SharedLists.class);

以下是我的 SharedLists pojo

 public class SharedLists {

@SerializedName("listId")
private String listId;

@SerializedName("listName")
private String listName;

@SerializedName("numberOfItems")
private int numberOfItems;

@SerializedName("collaborative")
private boolean collaborative;

@SerializedName("displaySettings")
private boolean displaySettings;

public int getNumberOfItems() {
return numberOfItems;
}
public void setNumberOfItems(int numberOfItems) {
this.numberOfItems = numberOfItems;
}
public boolean isCollaborative() {
return collaborative;
}
public void setCollaborative(boolean collaborative) {
this.collaborative = collaborative;
}
public boolean isDisplaySettings() {
return displaySettings;
}
public void setDisplaySettings(boolean displaySettings) {
this.displaySettings = displaySettings;
}

public String getListId() {
return listId;
}
public void setListId(String listId) {
this.listId = listId;
}

}

最佳答案

以下是您的 JSON 字符串。

{
"status": "FOUND",
"messages": null,
"sharedLists": [
{
"listId": "391647d",
"listName": "/???",
"numberOfItems": 0,
"colla borative": false,
"displaySettings": true
}
]
}

显然 sharedLists 是外部 JSON 对象内的 JSON 数组。

所以我有两个类,如下(通过提供 JSON 作为输入从 http://www.jsonschema2pojo.org/ 创建)

ResponseObject - 表示外部对象

 public class ResponseObject {

@SerializedName("status")
@Expose
private String status;
@SerializedName("messages")
@Expose
private Object messages;
@SerializedName("sharedLists")
@Expose
private List<SharedList> sharedLists = null;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Object getMessages() {
return messages;
}

public void setMessages(Object messages) {
this.messages = messages;
}

public List<SharedList> getSharedLists() {
return sharedLists;
}

public void setSharedLists(List<SharedList> sharedLists) {
this.sharedLists = sharedLists;
}

}

SharedList - 表示数组中的每个对象

public class SharedList {

@SerializedName("listId")
@Expose
private String listId;
@SerializedName("listName")
@Expose
private String listName;
@SerializedName("numberOfItems")
@Expose
private Integer numberOfItems;
@SerializedName("colla borative")
@Expose
private Boolean collaBorative;
@SerializedName("displaySettings")
@Expose
private Boolean displaySettings;

public String getListId() {
return listId;
}

public void setListId(String listId) {
this.listId = listId;
}

public String getListName() {
return listName;
}

public void setListName(String listName) {
this.listName = listName;
}

public Integer getNumberOfItems() {
return numberOfItems;
}

public void setNumberOfItems(Integer numberOfItems) {
this.numberOfItems = numberOfItems;
}

public Boolean getCollaBorative() {
return collaBorative;
}

public void setCollaBorative(Boolean collaBorative) {
this.collaBorative = collaBorative;
}

public Boolean getDisplaySettings() {
return displaySettings;
}

public void setDisplaySettings(Boolean displaySettings) {
this.displaySettings = displaySettings;
}

}

现在您可以使用 GSON 解析整个 JSON 字符串,如下所示

Gson gson = new Gson();
ResponseObject target = gson.fromJson(inputString, ResponseObject.class);

希望这有帮助。

关于java - 如何使用Java将Json字符串数组设置为Java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006902/

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