简体   繁体   English

GSON解析动态JSON字段

[英]GSON parsing dynamic JSON field

I can't seem to figure this out. 我似乎无法弄清楚这一点。 I've looked at a couple SO posts ( here , and here ), and my situation is just a little different. 我看了几个SO帖子( 这里这里 ),我的情况有点不同。

I'm not sure if I have to register a new TypeToken or what. 我不确定我是否必须注册一个新的TypeToken或什么。 But my JSON object looks like this: 但我的JSON对象如下所示:

{
    "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've created both of my objects like this: 我创建了两个像这样的对象:

public class Response {

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

Separate file: 单独的文件:

public class DateList {

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

I'm not sure how to tweek it to get it right. 我不确定如何调整它以使其正确。 Documentation doesn't seem to help... And the other examples I've seen are parsing a custom object, not a string type. 文档似乎没有帮助...我见过的其他示例是解析自定义对象,而不是字符串类型。

Thanks! 谢谢!

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 the Response.java 和Response.java

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

At least that seemed to work out of the box. 至少那似乎是开箱即用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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