2

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.