反序列化Gson中的嵌套对象

lyfkaqu1  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(165)

我的JSON响应如下:

{
  "General": {
    "Code": "xyz",
    "People": {
      "0": {
        "Name": "abc"
      },
      "1": {
        "Name": "def..."
      },
      "AddressData": {
        "Street": 123,
        "ZIP": 456
      }
    }
  }
}

对象1和2是相同的对象。它们总是具有相同的属性。但是,它们可能不仅仅是两个对象,这取决于我何时拉取JSON。我真的不确定如何使用Gson对数据进行反序列化。
我已尝试使用:

@SerializedName("People")
@Expose
private People[] People;

人员所在位置:

private class People {
    @SerializedName("Name")
    @Expose
    private String name;

    public People(String name){
        this.name = name
    }
}

然而,我得到的错误
java.lang.IllegalStateException:应为开始_ARRAY,但在第1行为BEGIN_OBJECT
我的普通类是:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class General {
    @SerializedName("Code")
    @Expose
    private String code;

    @SerializedName("People")
    @Expose
    private People[] people;

    @SerializedName("AddressData")
    @Expose
    private AddressData addressData;

    private class People {
        @SerializedName("Name")
        @Expose
        private String name;

        public People(String name){
            this.name = name;
        }
    }

    private class AddressData {
        @SerializedName("Street")
        @Expose
        private String street;

        @SerializedName("ZIP")
        @Expose
        private String zip;
    }
}
fnvucqvd

fnvucqvd1#

编写一个自定义序列化器。下面是问题中JSON的完整示例。

class PeopleSerializer implements JsonSerializer<People>, JsonDeserializer<People> {
    @Override
    public JsonElement serialize(People src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject obj = new JsonObject();
        for (Entry<Integer, PeopleName> entry : src.names.entrySet())
            obj.add(String.valueOf(entry.getKey()), context.serialize(entry.getValue()));
        obj.add("AddressData", context.serialize(src.addressData));
        return obj;
    }
    @Override
    public People deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        People people = new People();
        for (Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            if (entry.getKey().equals("AddressData"))
                people.addressData = context.deserialize(entry.getValue(), AddressData.class);
            else
                people.names.put(Integer.valueOf(entry.getKey()), context.deserialize(entry.getValue(), PeopleName.class));
        }
        return people;
    }
}
  • 测试 *
public class Test {
    public static void main(String[] args) throws Exception {
        String json = "{\r\n" + 
                      "  \"General\": {\r\n" + 
                      "    \"Code\": \"xyz\",\r\n" + 
                      "    \"People\": {\r\n" + 
                      "      \"0\": {\r\n" + 
                      "        \"Name\": \"abc\"\r\n" + 
                      "      },\r\n" + 
                      "      \"1\": {\r\n" + 
                      "        \"Name\": \"def...\"\r\n" + 
                      "      },\r\n" + 
                      "      \"AddressData\": {\r\n" + 
                      "        \"Street\": 123,\r\n" + 
                      "        \"ZIP\": 456\r\n" + 
                      "      }\r\n" + 
                      "    }\r\n" + 
                      "  }\r\n" + 
                      "}";
        Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                .create();

        Root root = gson.fromJson(json, Root.class);
        System.out.println(root);

        gson.toJson(root, System.out);
    }
}
class Root {
    General general;
    @Override
    public String toString() {
        return "Root[general=" + this.general + "]";
    }
}
class General {
    String code;
    People people;
    @Override
    public String toString() {
        return "General[code=" + this.code + ", people=" + this.people + "]";
    }
}
@JsonAdapter(PeopleSerializer.class)
class People {
    Map<Integer, PeopleName> names = new TreeMap<>();
    AddressData addressData;
    @Override
    public String toString() {
        return "People[names=" + this.names + ", addressData=" + this.addressData + "]";
    }
}
class PeopleName {
    String name;
    @Override
    public String toString() {
        return "PeopleName[name=" + this.name + "]";
    }
}
class AddressData {
    Integer street;
    @SerializedName("ZIP")
    Integer zip;
    @Override
    public String toString() {
        return "AddressData[street=" + this.street + ", zip=" + this.zip + "]";
    }
}
  • 输出 *
Root[general=General[code=xyz, people=People[names={0=PeopleName[name=abc], 1=PeopleName[name=def...]}, addressData=AddressData[street=123, zip=456]]]]
{"General":{"Code":"xyz","People":{"0":{"Name":"abc"},"1":{"Name":"def..."},"AddressData":{"Street":123,"ZIP":456}}}}
63lcw9qa

63lcw9qa2#

对象人是一张Map。对象1和2是关键。我现在要睡觉了:)

相关问题