2016-02-29 93 views
0

我需要建立在以下格式JSON响应:有没有更好的方法来实现HashMap <String,List <HashMap <String,List <Details> >>>?

{"responseCode":"00", 
"responseMessage":"Success", 
"planList":[ 
    {"category":"category1", 
    "details":...} 
    {"category":"category2", 
    "details":...} 
    {"category":"category3", 
    "details":...} 
    ]} 

我想出了HashMap<String, List<HashMap<String, List<Details>>>>,但我觉得应该有一个更好的方式来做到这一点。请帮忙。

在此先感谢。

+0

创建您自己的类并封装复杂的数据结构。有一个列表的地图列表的地图不是一个好主意。从'HashMap '开始,其中'Response'是你自己的类,它包装了有hashmap(也可以分成两个类)的列表。 – Maroun

回答

6

我认为你需要创建具有必要字段的类。例如:

class MyResponse { 
    private String responseCode; 
    private String responseMessage; 
    private List<Category> categories; 
} 

class Category { 
    private String category; 
    private String details; 
} 

它会增加可读性。

0

您可以为列表中的不同实体编写类。

例如一个类响应:

class Response { 

    private String responseCode; 
    private String responseMessage; 

    private List<Category> categories; 
//.... 
} 

和一类分类:

class Category { 

    private String categoryName; 
    private List<Details> 
    //... 
} 

这应该与像GSON或杰克逊不同JSON-库工作。

0

您需要为每个模型元素创建单独的类并从这些类收集完整的响应,例如,

class Category {...} 

... 

another classes staff 

... 

class Response {...} 

其中响应 - 最终响应类;

相关问题