简体   繁体   English

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

[英]How do I consume an array of JSON objects with Spring Boot?

With reference to this guide: https://spring.io/guides/gs/consuming-rest/ 参考本指南: https : //spring.io/guides/gs/using-rest/

The guide shows how to consume a RESTful web service. 该指南显示了如何使用RESTful Web服务。

The response from the REST API query results in the following JSON: REST API查询的响应将导致以下JSON:

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

It creates a domain class called Quote.java to contain the data in the response: 它创建一个称为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 +
                '}';
    }
}

My questions is how do I represent the following json: 我的问题是如何表示以下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
}

The outer objects like size and limit are straightforward but I can't figure out how to represent the values object, which looks like an array of json objects. 外部对象(例如sizelimit很简单,但是我不知道如何表示values对象,它看起来像json对象数组。

This should work. 这应该工作。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Boot中使用数组中的数组? - how to consume an array within an array in spring boot? 如何将嵌套的 JSON object 转换为阵列 Spring 引导? - How do i turn a nested JSON object to an array Spring boot? 如何使用org.json使用数组数组? - How do I consume an array of arrays using org.json? 如何在简单的 Spring Boot 应用程序中使用嵌套的对象数组反序列化 Json - How to deserialize Json with a nested array of objects in a simple Spring Boot application 如何在 spring 引导中返回 json 格式的对象数组? - How to return an array of objects in json format in spring boot? Spring 引导 - 如何使用 Post 请求 (JSON) 发送对象数组 - Spring Boot - How to send an array of objects using Post request (JSON) 如何创建一个包含多个对象的json数组 java spring boot - How to create a json array with multiple objects java spring boot Spring 引导 - 返回 JSON 和对象数组 - Spring Boot - Returning JSON with array of objects 如何将外部 REST API 响应主体从 json/string 转换/使用到 bean - 我正在使用 Spring Boot-Maven - How do I convert/consume an external REST API response body from json/string to bean - I'm using Spring Boot-Maven 如何在 Spring 引导中将不同的 JSON 响应消耗到客户端的单个端点中? - How to consume different JSON response into a single endpoint for a client in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM