将嵌套对象显示为空的Gson

xe55xuns  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(169)

我试图将对象反序列化为JSON,一个对象包含嵌套对象。我创建了嵌套对象(UserAddress)和包含它的对象(UserObjectWithNestedObject)的示例。但是,当我序列化时,嵌套对象为空。所以我在调试器模式下运行程序,看看它何时变为空。
在示例化UserObjectWithNestedObject类时,当我将其用作字段时,它似乎为null,但我不知道为什么,因为我在同一作用域中的对象中示例化了它,所以您可能会认为它们能够毫无问题地进行通信


抱歉,您可能需要缩放
我的类
主要

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

 public class Main {

    public static void main(String[] args) {

        serializeUserObjectWithNestedObject();
    }

    public static void serializeUserObjectWithNestedObject(){

        UserAddress userAddress = new UserAddress(
                "std",
                "000",
                "Dulin",
                "Ireland"
        );

        UserObjectWithNestedObject uowno= new UserObjectWithNestedObject(

                "thanss",
                "j@f.com",
                21,
                true,
                userAddress
        );

        System.out.println( "\nthis is the UserAddress object "+userAddress);

        String theJson = new Gson().toJson(uowno, UserObjectWithNestedObject.class);

        System.out.println(theJson);
    }

}

具有嵌套对象的用户对象

public class UserObjectWithNestedObject {

    private String name;
    private String email;
    private int age;
    private boolean isDeveloper;

    //This is the object inside this object
    public UserAddress userAddress;

    public UserObjectWithNestedObject(String name, String email, int age, boolean isDeveloper, UserAddress userAddress) {
        this.name = name;
        this.email = email;
        this.age = age;
        this.isDeveloper = isDeveloper;
    }

    @Override
    public String toString() {
        return "UserObjectWithNestedObject{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                ", isDeveloper=" + isDeveloper +
                ", userAddress=" + userAddress +
                '}';
    }
}

UserAddress(这是变为空的嵌套对象)

public class UserAddress {

    private String street;
    private String houseNumber;
    private String city;
    private String country;

    public UserAddress( String houseNumber, String city, String country, String street) {
        this.houseNumber = houseNumber;
        this.city = city;
        this.country = country;
        this.street = street;

    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "UserAddress{" +
                "street='" + street + '\'' +
                ", houseNumber='" + houseNumber + '\'' +
                ", city='" + city + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}
p5cysglq

p5cysglq1#

您没有初始化它:)

public UserObjectWithNestedObject(String name, String email, int age, boolean isDeveloper, UserAddress userAddress) {
    this.name = name;
    this.email = email;
    this.age = age;
    this.isDeveloper = isDeveloper;
    this.userAddress = userAddress;
}

相关问题