简体   繁体   中英

How parse variable name JSON objects using GSON?

How can I parse JSON using GSON with variable object names? 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. 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

Thanks in advance!

Here's your class structure:

MyObject (main object):

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.

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