Partitioning a GSON Dynamic JSON Field

I can’t figure it out. I looked at a couple of SO posts ( here and here ), and my situation is just a little different.

I'm not sure if I need to register a new TypeToken or what. But my JSON object looks like this:

{ "id": 6, "error": "0", "dates": { 34234 : "2011-01-01" // I want to parse the date into a string. 87474 : "2011-08-09" // The first values are all unique. . //this can be any number of entries. . . 74857 : "2011-09-22" } } 

I created both of my objects:

 public class Response { public Integer id; public String error; public DateList dates; } 

Single file:

 public class DateList { public List<Map<Integer, String>> dateString; } 

I'm not sure how to tighten it so that everything is correct. The documentation doesn't seem to help ... And the other examples I saw analyze a user object, not a string type.

Thanks!

+6
source share
1 answer

I tried it in this form:

The json

 { "id": 6, "error": "0", "dates": { "34234" : "2011-01-01" "87474" : "2011-08-09" "74857" : "2011-09-22" } } 

And Response.java

 public class Response { public Integer id; public String error; public Map<Integer, String> dates; } 

At least it seemed to work out of the box.

+12
source

Source: https://habr.com/ru/post/898610/


All Articles