反序列化Gson中的嵌套对象

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

我的JSON响应如下:

  1. {
  2. "General": {
  3. "Code": "xyz",
  4. "People": {
  5. "0": {
  6. "Name": "abc"
  7. },
  8. "1": {
  9. "Name": "def..."
  10. },
  11. "AddressData": {
  12. "Street": 123,
  13. "ZIP": 456
  14. }
  15. }
  16. }
  17. }

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

  1. @SerializedName("People")
  2. @Expose
  3. private People[] People;

人员所在位置:

  1. private class People {
  2. @SerializedName("Name")
  3. @Expose
  4. private String name;
  5. public People(String name){
  6. this.name = name
  7. }
  8. }

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

  1. import com.google.gson.annotations.Expose;
  2. import com.google.gson.annotations.SerializedName;
  3. public class General {
  4. @SerializedName("Code")
  5. @Expose
  6. private String code;
  7. @SerializedName("People")
  8. @Expose
  9. private People[] people;
  10. @SerializedName("AddressData")
  11. @Expose
  12. private AddressData addressData;
  13. private class People {
  14. @SerializedName("Name")
  15. @Expose
  16. private String name;
  17. public People(String name){
  18. this.name = name;
  19. }
  20. }
  21. private class AddressData {
  22. @SerializedName("Street")
  23. @Expose
  24. private String street;
  25. @SerializedName("ZIP")
  26. @Expose
  27. private String zip;
  28. }
  29. }
fnvucqvd

fnvucqvd1#

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

  1. class PeopleSerializer implements JsonSerializer<People>, JsonDeserializer<People> {
  2. @Override
  3. public JsonElement serialize(People src, Type typeOfSrc, JsonSerializationContext context) {
  4. JsonObject obj = new JsonObject();
  5. for (Entry<Integer, PeopleName> entry : src.names.entrySet())
  6. obj.add(String.valueOf(entry.getKey()), context.serialize(entry.getValue()));
  7. obj.add("AddressData", context.serialize(src.addressData));
  8. return obj;
  9. }
  10. @Override
  11. public People deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  12. People people = new People();
  13. for (Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
  14. if (entry.getKey().equals("AddressData"))
  15. people.addressData = context.deserialize(entry.getValue(), AddressData.class);
  16. else
  17. people.names.put(Integer.valueOf(entry.getKey()), context.deserialize(entry.getValue(), PeopleName.class));
  18. }
  19. return people;
  20. }
  21. }
  • 测试 *
  1. public class Test {
  2. public static void main(String[] args) throws Exception {
  3. String json = "{\r\n" +
  4. " \"General\": {\r\n" +
  5. " \"Code\": \"xyz\",\r\n" +
  6. " \"People\": {\r\n" +
  7. " \"0\": {\r\n" +
  8. " \"Name\": \"abc\"\r\n" +
  9. " },\r\n" +
  10. " \"1\": {\r\n" +
  11. " \"Name\": \"def...\"\r\n" +
  12. " },\r\n" +
  13. " \"AddressData\": {\r\n" +
  14. " \"Street\": 123,\r\n" +
  15. " \"ZIP\": 456\r\n" +
  16. " }\r\n" +
  17. " }\r\n" +
  18. " }\r\n" +
  19. "}";
  20. Gson gson = new GsonBuilder()
  21. .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
  22. .create();
  23. Root root = gson.fromJson(json, Root.class);
  24. System.out.println(root);
  25. gson.toJson(root, System.out);
  26. }
  27. }
  28. class Root {
  29. General general;
  30. @Override
  31. public String toString() {
  32. return "Root[general=" + this.general + "]";
  33. }
  34. }
  35. class General {
  36. String code;
  37. People people;
  38. @Override
  39. public String toString() {
  40. return "General[code=" + this.code + ", people=" + this.people + "]";
  41. }
  42. }
  43. @JsonAdapter(PeopleSerializer.class)
  44. class People {
  45. Map<Integer, PeopleName> names = new TreeMap<>();
  46. AddressData addressData;
  47. @Override
  48. public String toString() {
  49. return "People[names=" + this.names + ", addressData=" + this.addressData + "]";
  50. }
  51. }
  52. class PeopleName {
  53. String name;
  54. @Override
  55. public String toString() {
  56. return "PeopleName[name=" + this.name + "]";
  57. }
  58. }
  59. class AddressData {
  60. Integer street;
  61. @SerializedName("ZIP")
  62. Integer zip;
  63. @Override
  64. public String toString() {
  65. return "AddressData[street=" + this.street + ", zip=" + this.zip + "]";
  66. }
  67. }
  • 输出 *
  1. Root[general=General[code=xyz, people=People[names={0=PeopleName[name=abc], 1=PeopleName[name=def...]}, addressData=AddressData[street=123, zip=456]]]]
  2. {"General":{"Code":"xyz","People":{"0":{"Name":"abc"},"1":{"Name":"def..."},"AddressData":{"Street":123,"ZIP":456}}}}
展开查看全部
63lcw9qa

63lcw9qa2#

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

相关问题