简体   繁体   中英

Is there a better way to implement HashMap<String, List<HashMap<String, List<Details>>>>?

I need to create json response in the following format:

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

I came up with HashMap<String, List<HashMap<String, List<Details>>>> , but I feel there should be a better way to do this. Please help.

Thanks in advance.

I think you need to create classes with needed fields. For example:

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

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

It will increase readability.

You could write classes for the different entities in your list.

For example a class Response:

class Response {

     private String responseCode;
     private String responseMessage;

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

and a class Category:

class Category {

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

This should work with different json-libraries like Gson or Jackson.

You need to create separate classes for each model element and than collect full response from those clases, eg

class Category {...}

...

another classes staff

...

class Response {...}

where Response - final response class;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM