如何使用Spring Boot使用JSON对象数组?

时间:2018-02-15 10:37:24

标签: java json spring-boot

参考本指南: https://spring.io/guides/gs/consuming-rest/

该指南介绍了如何使用RESTful Web服务。

REST API查询的响应产生以下JSON:

{
   type: "success",
   value: {
      id: 10,
      quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
   }
}

它创建一个名为Quote.java的域类,以包含响应中的数据:

package hello;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }
}

我的问题是如何表示以下json:

{
    "size": 1,
    "limit": 25,
    "isLastPage": true,
    "values": [
        {
            "user": {
                "name": "jcitizen",
                "emailAddress": "jane@example.com",
                "id": 101,
                "displayName": "Jane Citizen",
                "active": true,
                "slug": "jcitizen",
                "type": "NORMAL"
            },
            "permission": "ADMIN"
        }
    ],
    "start": 0
}

sizelimit这样的外部对象很简单,但我无法弄清楚如何表示values对象,它看起来像一个json对象数组。

1 个答案:

答案 0 :(得分:2)

这应该有效。

class Output {
    private String size,
    private int limit;
    private boolean isLastPage,
    private List<Value> values;
    private int start ;
 }


class Value 
 {
     User user,
     private String permission;
 }

class User {
    private String name,
    private String emailAddress,
    private int id,
    private String displayName,
    private boolean active,
    private String slug,
    private String type
}