-1

I am working on android application which shows the railways available in two stations.So, I am fetching the railway data from railwayapi to show information on android app.But I am not able to convert that JSON data.I am also having difficulties in creating object class for that. so, I need a help to convert that data.Any help will be appreciated.

I am giving the JSON data below,

{
    "debit": 1,
        "total": 3,
        "response_code": 200,
        "trains": [
    {
        "days": [
        {
            "code": "MON",
                "runs": "Y"
        },
        {
            "code": "TUE",
                "runs": "Y"
        },
        {
            "code": "WED",
                "runs": "Y"
        },
        {
            "code": "THU",
                "runs": "Y"
        },
        {
            "code": "FRI",
                "runs": "Y"
        },
        {
            "code": "SAT",
                "runs": "Y"
        },
        {
            "code": "SUN",
                "runs": "Y"
        }
        ],
        "number": "11030",
            "dest_arrival_time": "20:05",
            "src_departure_time": "07:55",
            "classes": [
        {
            "name": "FIRST CLASS",
                "code": "FC"
        },
        {
            "name": "SECOND SEATING",
                "code": "2S"
        },
        {
            "name": "AC CHAIR CAR",
                "code": "CC"
        },
        {
            "name": "SLEEPER CLASS",
                "code": "SL"
        },
        {
            "name": "FIRST AC",
                "code": "1A"
        },
        {
            "name": "THIRD AC",
                "code": "3A"
        },
        {
            "name": "SECOND AC",
                "code": "2A"
        },
        {
            "name": "3rd AC ECONOMY",
                "code": "3E"
        }
        ],
        "name": "KOYNA EXPRESS",
            "to_station": {
        "name": "C SHIVAJI MAHARAJ T",
                "code": "CSMT",
                "lng": 72.87033,
                "lat": 19.054346
    },
        "from_station": {
        "name": "KOLHAPUR",
                "code": "KOP",
                "lng": 74.24053,
                "lat": 16.70284
    },
        "travel_time": "12:10"
    },
    {
        "days": [
        {
            "code": "MON",
                "runs": "Y"
        },
        {
            "code": "TUE",
                "runs": "Y"
        },
        {
            "code": "WED",
                "runs": "Y"
        },
        {
            "code": "THU",
                "runs": "Y"
        },
        {
            "code": "FRI",
                "runs": "Y"
        },
        {
            "code": "SAT",
                "runs": "Y"
        },
        {
            "code": "SUN",
                "runs": "Y"
        }
        ],
        "number": "17412",
            "dest_arrival_time": "07:25",
            "src_departure_time": "20:30",
            "classes": [
        {
            "name": "FIRST CLASS",
                "code": "FC"
        },
        {
            "name": "SECOND SEATING",
                "code": "2S"
        },
        {
            "name": "AC CHAIR CAR",
                "code": "CC"
        },
        {
            "name": "SLEEPER CLASS",
                "code": "SL"
        },
        {
            "name": "FIRST AC",
                "code": "1A"
        },
        {
            "name": "THIRD AC",
                "code": "3A"
        },
        {
            "name": "SECOND AC",
                "code": "2A"
        },
        {
            "name": "3rd AC ECONOMY",
                "code": "3E"
        }
        ],
        "name": "MAHALAXMI EXP",
            "to_station": {
        "name": "C SHIVAJI MAHARAJ T",
                "code": "CSMT",
                "lng": 72.87033,
                "lat": 19.054346
    },
        "from_station": {
        "name": "KOLHAPUR",
                "code": "KOP",
                "lng": 74.24053,
                "lat": 16.70284
    },
        "travel_time": "10:55"
    },
    {
        "days": [
        {
            "code": "MON",
                "runs": "Y"
        },
        {
            "code": "TUE",
                "runs": "Y"
        },
        {
            "code": "WED",
                "runs": "Y"
        },
        {
            "code": "THU",
                "runs": "Y"
        },
        {
            "code": "FRI",
                "runs": "Y"
        },
        {
            "code": "SAT",
                "runs": "Y"
        },
        {
            "code": "SUN",
                "runs": "Y"
        }
        ],
        "number": "11024",
            "dest_arrival_time": "11:50",
            "src_departure_time": "22:50",
            "classes": [
        {
            "name": "FIRST CLASS",
                "code": "FC"
        },
        {
            "name": "SECOND SEATING",
                "code": "2S"
        },
        {
            "name": "AC CHAIR CAR",
                "code": "CC"
        },
        {
            "name": "SLEEPER CLASS",
                "code": "SL"
        },
        {
            "name": "FIRST AC",
                "code": "1A"
        },
        {
            "name": "THIRD AC",
                "code": "3A"
        },
        {
            "name": "SECOND AC",
                "code": "2A"
        },
        {
            "name": "3rd AC ECONOMY",
                "code": "3E"
        }
        ],
        "name": "SAHYADRI EXP",
            "to_station": {
        "name": "C SHIVAJI MAHARAJ T",
                "code": "CSMT",
                "lng": 72.87033,
                "lat": 19.054346
    },
        "from_station": {
        "name": "KOLHAPUR",
                "code": "KOP",
                "lng": 74.24053,
                "lat": 16.70284
    },
        "travel_time": "13:00"
    }
    ]
}

Thanks for any help.

minfo
  • 109
  • 1
  • 12
  • Use http://www.jsonschema2pojo.org/ for coverting json to PoJo and learn how to use gson - http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html – Jobin Lawrance Apr 14 '18 at 07:14
  • 2
    Possible duplicate of [How to convert the following json string to java object?](https://stackoverflow.com/questions/10308452/how-to-convert-the-following-json-string-to-java-object) – AmirHossein Rd Apr 14 '18 at 07:17
  • use http://jsoneditoronline.org/ to see json in simple way and then you can get it done – Janku Apr 14 '18 at 07:19
  • Use this https://www.site24x7.com/tools/json-to-java.html to get the appropriate Java model class for your json. Now add Gson to your build the gradle. And then YourModelClassName obj=new Gson().fromJson(jsonString,YourModelClassName.class); will give you the appropriate data – Ashish Kumar Apr 14 '18 at 07:20
  • Thaks Jobin Lawrance. I got the all Pojo classes, now I can easily process that data. – minfo Apr 14 '18 at 09:22

2 Answers2

0

There is a no way without parsing a response you got a data but you get that by using different techniques.

You can try using Gson , Gson1 or Jackson

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

You need to read google Gson for parsing the Json response. And for converting Json response to POJO class you need to see this

Hitesh Sarsava
  • 666
  • 4
  • 15