Chirag Patel
Chirag Patel

Reputation: 1611

How to create Parcelable class using following JSON Data?

Array key will be change every time when you call api so how to make parcel class.

 {
        "status": 200,
        "message": "Ground Schedules",
        "data": {
            "Schedules": {
                "2017-05-04": [
                    {
                        "id": "1216",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "02:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1258",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "02:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1259",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "04:00:00",
                        "end_time": "06:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1215",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "24:00:00",
                        "end_time": "02:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ],
                "2017-05-05": [
                    {
                        "id": "1266",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "03:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ],
                "2017-05-06": [
                    {
                        "id": "1268",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "01:00:00",
                        "end_time": "02:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1267",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "24:00:00",
                        "end_time": "01:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ]
            },
            "GroundBookedSlots": [
                {
                    "id": "1120",
                    "ground_id": "100",
                    "ground_court_id": "71",
                    "start_time": "17:00:00",
                    "end_time": "18:00:00",
                    "price": "500.00",
                    "date": "2017-04-19"
                },
                {
                    "id": "1112",
                    "ground_id": "100",
                    "ground_court_id": "71",
                    "start_time": "17:00:00",
                    "end_time": "18:00:00",
                    "price": "500.00",
                    "date": "2017-04-18"
                }
            ]
        }
    }

Upvotes: 1

Views: 515

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Your Json Array is Dynamic . Use Iterator

To use an iterator, follow these steps −

  1. Obtain an iterator to the start of the collection by calling the collection's iterator( ) method.
  2. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true.
  3. Within the loop, obtain each element by calling next( ).

At first, Get Key Value

final JSONObject getJson = JsonObject.getJSONObject("Schedules");
                Iterator  iteratorObj = getJson.keys();
                ArrayList<String> al_getAllArray=new ArrayList<String>();
                while (iteratorObj.hasNext())
                {
                    String getJsonArray = (String)iteratorObj.next();
                    System.out.println("Key: " + Key + "------>" + getJsonArray );
                    al_getAllArray.add(getJsonArray);
                    .....//do your work//.......

                }

Upvotes: 2

Related Questions