在我当前的项目中,我在 android 中使用 GSON 库,并且遇到了嵌套 Maps 反序列化的问题。这就是初始 json 的样子
{
"5":{
"id":5,
"name":"initial name",
"image_url":"uploads/71d44b5247cc1a7c56e62fa51ca91d9b.png",
"status":"1",
"flowers":{
"7":{
"id":7,
"category_id":"5",
"name":"test",
"description":"some description",
"price":"1000",
"image_url":"uploads/test.png",
"status":"1",
"color":"red",
}
}
}
}
还有我的pojo
class Category {
long id;
String name;
String image_url;
HashMap<String,Flower> flowers;
}
和花类
class Flower {
long id;
String category_id;
String name;
String description;
String price;
String image_url;
String status;
}
但是当我尝试反序列化这些对象时,我可以访问嵌套的哈希图,示例代码是
public class TestJson {
public static void main(String[] args) {
Gson gson = new Gson();
try {
BufferedReader br = new BufferedReader(
new FileReader("2.txt"));
HashMap<String,Category> map = gson.fromJson(br, HashMap.class);
Collection<Category> asd = map.values();
System.out.println(map.values());
} catch (IOException e) {
e.printStackTrace();
}
}
}
有什么建议么?