简体   繁体   English

如何使用 GSON 解析变量名 JSON 对象?

[英]How parse variable name JSON objects using GSON?

How can I parse JSON using GSON with variable object names?如何使用具有可变对象名称的 GSON 解析 JSON? The "routes" objects has the same structure, but different name. “路由”对象具有相同的结构,但名称不同。 It has many different names because it reflects to travel lines.它有许多不同的名称,因为它反映了旅行线路。 I'm trying to read it directly to Java class (Android, Retrofit), but I wouldn't create single class for all travel lines in Budapest.我正在尝试将其直接读取到 Java 类(Android、Retrofit),但我不会为布达佩斯的所有旅游线路创建单个类。 Is it possible to read it somehow?是否有可能以某种方式阅读它?

{
"version": 2,
"status": "OK",
"code": 200,
"text": "OK",
"currentTime": 1448881433747,
"data": {
    "limitExceeded": false,
    "references": {
        "routes": {
            "BKK_9630": {
                "id": "BKK_9630",
                "shortName": "963",
                "longName": null,
                "description": "Hűvösvölgy | Nagykovácsi, Tisza István tér",
                "type": "BUS",
                "url": null,
                "color": "1E1E1E",
                "textColor": "FFFFFF",
                "agencyId": "BKK",
                "bikesAllowed": false
            },
            "BKK_0630": {
                "id": "BKK_0630",
                "shortName": "63",
                "longName": null,
                "description": "Hűvösvölgy | Nagykovácsi, Tisza István tér",
                "type": "BUS",
                "url": null,
                "color": "009FE3",
                "textColor": "FFFFFF",
                "agencyId": "BKK",
                "bikesAllowed": false
            }
        },
        "trips": {},
        "alerts": {}
    }
}
}

The full JSON response: http://futar.bkk.hu/bkk-utvonaltervezo-api/ws/otp/api/where/search.json?query=Erd%C3%A9szh%C3%A1z完整的 JSON 响应: http : //futar.bkk.hu/bkk-utvonaltervezo-api/ws/otp/api/where/search.json?query=Erd%C3%A9szh%C3%A1z

Thanks in advance!提前致谢!

Here's your class structure:这是您的类结构:

MyObject (main object): MyObject (主要对象):

public class MyObject{

    private Integer version;
    private String status;
    private Integer code;
    private Data data;
}

Data :数据

public class Data{

    private boolean limitExceeded;
    private References references;
}

References :参考资料

public class References{

    private Map<String, Route> routes;
}

Route :路线:

public class Route{

    private String shortName;
}

And then:进而:

String json = "{'version':2,'status':'OK','code':200,'text':'OK','currentTime':1448881433747,'data':{'limitExceeded':false,'references':{'routes':{'BKK_9630':{'id':'BKK_9630','shortName':'963','longName':null,'description':'Hűvösvölgy | Nagykovácsi, Tisza István tér','type':'BUS','url':null,'color':'1E1E1E','textColor':'FFFFFF','agencyId':'BKK','bikesAllowed':false},'BKK_0630':{'id':'BKK_0630','shortName':'63','longName':null,'description':'Hűvösvölgy | Nagykovácsi, Tisza István tér','type':'BUS','url':null,'color':'009FE3','textColor':'FFFFFF','agencyId':'BKK','bikesAllowed':false}},'trips':{},'alerts':{}}}}";
        Gson gson = new Gson();
        MyObject fromJson = gson.fromJson( json, MyObject.class );
        System.out.println( fromJson );

Result:结果:

MyObject [version=2, status=OK, code=200, data=Data [limitExceeded=false, references=References [routes={BKK_9630=Route [shortName=963], BKK_0630=Route [shortName=63]}]]]

Note that, I didn't write all fields you have to write them.请注意,我没有写出您必须编写的所有字段。 Also don't forget to create getter and setters and toString overrides.也不要忘记创建getter and setters以及toString覆盖。

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

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