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

时间:2017-05-16 16:29:51

标签: java json gson

我有以下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;
}

} 

1 个答案:

答案 0 :(得分:1)

以下是您的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);

希望这有帮助。