在 Spring Boot 应用程序中解析 JSON [英] Parsing JSON in Spring boot application

查看:47
本文介绍了在 Spring Boot 应用程序中解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想办法解析这种类型的 JSON 时遇到了麻烦.在我的应用程序中收到此响应后,我需要能够分别访问数据和消息.

I'm having trouble in thinking up a way of parsing this type of JSON. I need to be able to access both data and message separately after getting this response inside my application.

{
    "data": [
        {
            "email": "eskaferas@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        },
        {
            "email": "Soras@gmail.com",
            "firstName": "Oras",
            "lastName": "Moras"
        },
        {
            "email": "bzbzb@gmail.com",
            "firstName": "hello",
            "lastName": "bye"
        },
        {
            "email": "lrc@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        }
    ],
    "message": "Success"
}

有人可以建议专门用于 Spring Boot 的方法吗?或者Spring boot中常用的一种解析这种JSON的方式.

Could anyone suggest a method specifically for Spring boot? Or a common way that is used in Spring boot to parse this type of JSON.

谢谢.

编辑

我的主要问题是 Spring Boot 可以使用什么工具,如果我使用 Jackson,我的 POJO 类会是什么样子.会是这样吗?:

My main questions would be just what tool can be used for Spring Boot and what would my POJO class look like if I used Jackson. Would it be something like this?:

public class testPojo {
  Users[] users;
  String message;
}

推荐答案

Pojo 类将如下所示:

Pojo class will look like:

public class TestPojo{

    private Users[] data;
    private String message;

    public Users[] getData() {
        return data;
    }

    public void setData(Users[] data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
     }
}

class Users {
    private String email;
    private String firstName;
    private String lastName;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

在控制器的方法参数中使用@RequestBody.

use @RequestBody in your controller's method parameter.

@RestController
class AbcController{

    PostMapping("/api")
    public String create(@RequestBody TestPojo test){
    // you can acess your json in test object
    return "done"
    } 
}

这篇关于在 Spring Boot 应用程序中解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆