使用 Jackson 将 java 类映射到 JSON

发布于 2025-01-16 20:16:42 字数 278 浏览 1 评论 0原文

我想使用 Jackson 库将以下 bean 类转换为 JSON 对象

public class Student {
String name ;
int id ;
List<Address> address;
}

我想要以下 json

{
  "Name" : "sys1",
  "Id" : 1,
  "address" : [some address]
}

任何人都可以帮助我如何实现这一目标吗?

I want to convert the following bean class to JSON object by using Jackson library

public class Student {
String name ;
int id ;
List<Address> address;
}

I want following json

{
  "Name" : "sys1",
  "Id" : 1,
  "address" : [some address]
}

Can anyone help me how to achieve this ?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2025-01-23 20:16:42
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JavaToJson {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Address> addrList = new ArrayList<>();
            Address addr = new Address();
            addr.setArea("ABC");
            addr.setCity("XYZ");
            addrList.add(addr);

            Student std = new Student();
            std.setName("Rahul");
            std.setId(1);
            std.setAddress(addrList);

            String json = objectMapper.writeValueAsString(std);
            System.out.println(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


class Student {
    String name;
    int id;
    List<Address> address;

    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public List<Address> getAddress() {
        return address;
    }
    public void setAddress(List<Address> address) {
        this.address = address;
    }
}

class Address {
    String area;
    String city;
    public String getArea() {
        return this.area;
    }
    public void setArea(String area) {
        this.area = area;
    }
    public String getCity() {
        return this.area;
    }
    public void setCity(String city) {
        this.city = city;
    }
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JavaToJson {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Address> addrList = new ArrayList<>();
            Address addr = new Address();
            addr.setArea("ABC");
            addr.setCity("XYZ");
            addrList.add(addr);

            Student std = new Student();
            std.setName("Rahul");
            std.setId(1);
            std.setAddress(addrList);

            String json = objectMapper.writeValueAsString(std);
            System.out.println(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


class Student {
    String name;
    int id;
    List<Address> address;

    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public List<Address> getAddress() {
        return address;
    }
    public void setAddress(List<Address> address) {
        this.address = address;
    }
}

class Address {
    String area;
    String city;
    public String getArea() {
        return this.area;
    }
    public void setArea(String area) {
        this.area = area;
    }
    public String getCity() {
        return this.area;
    }
    public void setCity(String city) {
        this.city = city;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文