Gson解析-改造- Android [已关闭]

kmynzznz  于 2022-11-06  发布在  Android
关注(0)|答案(2)|浏览(247)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

去年关闭了。
Improve this question

  1. {
  2. "products": [{
  3. "electronics": {
  4. "productId": "A2",
  5. "title": "earphone",
  6. "productDescription": "Description",
  7. "imageUrls": {
  8. "url1": "url1",
  9. "url2": "url2",
  10. "url3": "url3"
  11. }
  12. },
  13. "electronics": {
  14. "productId": "A2",
  15. "title": "mic",
  16. "productDescription": "Description",
  17. "imageUrls": {
  18. "url1": "url1",
  19. "url2": "url2",
  20. "url3": "url3"
  21. }
  22. }
  23. }
  24. ]
  25. }

Package 函数模型类(& M):

  1. public class Product {
  2. @SerializedName("productId")
  3. private String productId;
  4. @SerializedName("title")
  5. private String title;
  6. @SerializedName("productDescription")
  7. private String productDescription;
  8. @SerializedName("imageUrls")
  9. private List<ImageUrl> imageUrls;
  10. // Setters & Getters
  11. }
  12. public class ImageUrl {
  13. @SerializedName("url1")
  14. private String url1;
  15. @SerializedName("url2")
  16. private String url2;
  17. @SerializedName("url3")
  18. private String url3;
  19. // Setters & Getters
  20. }
  21. public class ProductWrapper {
  22. @SerializedName("electronics")
  23. private List<Product> mData;
  24. // Setters & Getters
  25. }

解串器:

  1. public class MyDeserializer<T> implements JsonDeserializer<T> {
  2. @Override
  3. public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
  4. // Get the "products" element from the parsed JSON
  5. JsonElement products = jsonElement.getAsJsonObject().getAsJsonArray("products");
  6. // Deserialize it. You use a new instance of Gson to avoid infinite recursion
  7. // to this deserializer
  8. return new Gson().fromJson(products, type);
  9. }
  10. }

改造示例

  1. Gson gson =
  2. new GsonBuilder()
  3. .registerTypeAdapter(Blog.class, new MyDeserializer())
  4. .create();
  5. retrofit = new Retrofit
  6. .Builder()
  7. .baseUrl(BASE_URL)
  8. .addConverterFactory(GsonConverterFactory.create(gson))
  9. .build();

以上代码不起作用。

daupos2t

daupos2t1#

我建议在这种情况下,你可以使用一些实用工具,如:https://www.jsonschema2pojo.org/
在这里你可以粘贴你的JSON。选择“源类型:“-〉JSON选择“注解样式:“-〉GSON
点击预览,您将看到以下POJO类结构,它将以正确方式生成

  1. -----------------------------------com.example.Electronics.java-----------------------------------
  2. package com.example;
  3. import javax.annotation.Generated;
  4. import com.google.gson.annotations.Expose;
  5. import com.google.gson.annotations.SerializedName;
  6. @Generated("jsonschema2pojo")
  7. public class Electronics {
  8. @SerializedName("productId")
  9. @Expose
  10. private String productId;
  11. @SerializedName("title")
  12. @Expose
  13. private String title;
  14. @SerializedName("productDescription")
  15. @Expose
  16. private String productDescription;
  17. @SerializedName("imageUrls")
  18. @Expose
  19. private ImageUrls imageUrls;
  20. public String getProductId() {
  21. return productId;
  22. }
  23. public void setProductId(String productId) {
  24. this.productId = productId;
  25. }
  26. public String getTitle() {
  27. return title;
  28. }
  29. public void setTitle(String title) {
  30. this.title = title;
  31. }
  32. public String getProductDescription() {
  33. return productDescription;
  34. }
  35. public void setProductDescription(String productDescription) {
  36. this.productDescription = productDescription;
  37. }
  38. public ImageUrls getImageUrls() {
  39. return imageUrls;
  40. }
  41. public void setImageUrls(ImageUrls imageUrls) {
  42. this.imageUrls = imageUrls;
  43. }
  44. }
  45. -----------------------------------com.example.Example.java-----------------------------------
  46. package com.example;
  47. import java.util.List;
  48. import javax.annotation.Generated;
  49. import com.google.gson.annotations.Expose;
  50. import com.google.gson.annotations.SerializedName;
  51. @Generated("jsonschema2pojo")
  52. public class Example {
  53. @SerializedName("products")
  54. @Expose
  55. private List<Product> products = null;
  56. public List<Product> getProducts() {
  57. return products;
  58. }
  59. public void setProducts(List<Product> products) {
  60. this.products = products;
  61. }
  62. }
  63. -----------------------------------com.example.ImageUrls.java-----------------------------------
  64. package com.example;
  65. import javax.annotation.Generated;
  66. import com.google.gson.annotations.Expose;
  67. import com.google.gson.annotations.SerializedName;
  68. @Generated("jsonschema2pojo")
  69. public class ImageUrls {
  70. @SerializedName("url1")
  71. @Expose
  72. private String url1;
  73. @SerializedName("url2")
  74. @Expose
  75. private String url2;
  76. @SerializedName("url3")
  77. @Expose
  78. private String url3;
  79. public String getUrl1() {
  80. return url1;
  81. }
  82. public void setUrl1(String url1) {
  83. this.url1 = url1;
  84. }
  85. public String getUrl2() {
  86. return url2;
  87. }
  88. public void setUrl2(String url2) {
  89. this.url2 = url2;
  90. }
  91. public String getUrl3() {
  92. return url3;
  93. }
  94. public void setUrl3(String url3) {
  95. this.url3 = url3;
  96. }
  97. }
  98. -----------------------------------com.example.Product.java-----------------------------------
  99. package com.example;
  100. import javax.annotation.Generated;
  101. import com.google.gson.annotations.Expose;
  102. import com.google.gson.annotations.SerializedName;
  103. @Generated("jsonschema2pojo")
  104. public class Product {
  105. @SerializedName("electronics")
  106. @Expose
  107. private Electronics electronics;
  108. public Electronics getElectronics() {
  109. return electronics;
  110. }
  111. public void setElectronics(Electronics electronics) {
  112. this.electronics = electronics;
  113. }
  114. }

然后创建Retrofit示例,您将只需要这个。

  1. retrofit = new Retrofit
  2. .Builder()
  3. .baseUrl(BASE_URL)
  4. .addConverterFactory(GsonConverterFactory.create())
  5. .build();

在本例中,您对改型调用的响应类型将是Example类,即关键字products所在的根类。

展开查看全部
bvjveswy

bvjveswy2#

根据您发布的代码,类ProductWrapper需要一个Blogs列表,但是在传入的JSON中,有一个产品列表。因此,将mData更改为:private List<Product> mData;
另外,现在还不需要自定义反序列化。如上所述,您应该尝试使用一个工具。如果您想使用Kotlin,可以使用一个类似的工具:https://www.json2kotlin.com/

相关问题