Java – gson parsing dynamic JSON fields

I can't seem to figure that out I read several so posts (here and here) and my situation is a little different

I'm not sure if I have to register a new typetoken or something But my JSON object is as follows:

{
    "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 two objects like this:

public class Response {

    public Integer id;
    public String error;
    public DateList dates;
}

Separate documents:

public class DateList {

    public List<Map<Integer,String>> dateString;
}

I'm not sure how to adjust it to make it right The documentation doesn't seem to help... Other examples I've seen are parsing custom objects, not string types

thank you!

Solution

I've tried this form:

Jason

{
    "id": 6,"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 seems to be out of the box

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>