0

I need some help to deserialize this JSON Response to array which I can use further

Json Array response that I am getting has two entities tradingHours and shutDownHours

Creating a Json reader for the Json Response and , looping in the reader until I get to shutDown node and then try to get the details using ShutdownHours class

"I cannot create a gson object from JSON response using the Trading Hours class , what am I missing here

JSON RESPONSE

{
    "licenceNumber": "123319",
    "tradingHours": [
        {
            "effectiveStartDate": "1947-12-01T00:00:00",
            "effectiveEndDate": "2008-06-30T00:00:00",
            "details": [
                {
                    "day": "Monday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Tuesday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Wednesday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Thursday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Friday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Saturday",
                    "startTime": "5:00 AM",
                    "endTime": "12:00 midnight"
                },
                {
                    "day": "Sunday",
                    "startTime": "10:00 AM",
                    "endTime": "11:00 PM"
                },
                {
                    "day": "Good Friday",
                    "startTime": "10:00 AM",
                    "endTime": "10:00 PM"
                },
                {
                    "day": "Christmas Day",
                    "startTime": "10:00 AM",
                    "endTime": "10:00 PM"
                },
                {
                    "day": "December 31st",
                    "startTime": "10:00 AM",
                    "endTime": "10:00 PM"
                }
            ]
        },
        {
            "effectiveStartDate": "2008-07-01T00:00:00",
            "effectiveEndDate": null,
            "details": [
                {
                    "day": "Monday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Tuesday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Wednesday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Thursday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Friday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Saturday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Sunday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Good Friday",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "December 24th",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "Christmas Day",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                },
                {
                    "day": "December 31st",
                    "startTime": "5:00 AM",
                    "endTime": "5:00 AM"
                }
            ]
        }
    ],
    "shutdownHours": [
        {
            "effectiveStartDate": "2003-02-27T00:00:00",
            "isPrimary": "Y",
            "details": [
                {
                    "day": "Monday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Tuesday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Wednesday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Thursday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Friday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Saturday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "Sunday",
                    "startTime": "2:00 AM",
                    "endTime": "8:00 AM"
                },
                {
                    "day": "PublicHoliday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                }
            ]
        },
        {
            "effectiveStartDate": "1947-12-01T00:00:00",
            "isPrimary": null,
            "details": [
                {
                    "day": "Monday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Tuesday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Wednesday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Thursday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Friday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Saturday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "Sunday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                },
                {
                    "day": "PublicHoliday",
                    "startTime": "4:00 AM",
                    "endTime": "10:00 AM"
                }
            ]
        }
    ]
}

Java CODE

public static TradingHours readTradingHours(JsonReader reader) throws IOException {
      ShutdownHours tradingHours = null;
      reader.beginObject();
      Gson gson = new Gson();
      while (reader.hasNext()) {

         String name = reader.nextName();
         if (name.equals("tradingHours")) {
            reader.beginArray();
            tradingHours = gson.fromJson(reader, ShutdownHours.class);
         }
         else {
            reader.skipValue();

         }
      }

      return tradingHours;
   }
TRADING HOURS CLASS , This class is used to create an object and is being used to create gson
    package com.tattsgroup.site.entity.model;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.List;

public class TradingHours implements Serializable {
    @SerializedName("effectiveStartDate")
    private String effectiveStartDate;

    @SerializedName("effectiveEndDate")
    private String getEffectiveEndDate;

    @SerializedName("details")
    private List<TradingHourDetails> tradingHoursDetails;

    public TradingHours() {
    }

    public TradingHours(String effectiveStartDate, String getEffectiveEndDate, List<TradingHourDetails> tradingHoursDetails) {

        this.effectiveStartDate = effectiveStartDate;
        this.getEffectiveEndDate = getEffectiveEndDate;
        this.tradingHoursDetails = tradingHoursDetails;
    }

    public String getEffectiveStartDate() {
        return effectiveStartDate;
    }

    public void setEffectiveStartDate(String effectiveStartDate) {
        this.effectiveStartDate = effectiveStartDate;
    }

    public String getGetEffectiveEndDate() {
        return getEffectiveEndDate;
    }

    public void setGetEffectiveEndDate(String getEffectiveEndDate) {
        this.getEffectiveEndDate = getEffectiveEndDate;
    }

    public List<TradingHourDetails> getTradingHoursDetails() {
        return tradingHoursDetails;
    }

    public void setTradingHoursDetails(List<TradingHourDetails> tradingHoursDetails) {
        this.tradingHoursDetails = tradingHoursDetails;
    }
}
3
  • are you using jackson or moxy Commented Feb 16, 2018 at 6:01
  • What exactly do you want to do...I'm unable to understand the problem here...This json can converted to a Class Structure easily...what more do you want? Commented Feb 16, 2018 at 6:36
  • @utkarsh31 "I cannot create a gson object from JSON response using the Trading Hours class , what am I missing here Commented Feb 18, 2018 at 23:44

1 Answer 1

1

I assume that you do not have to use JsonReader and just want to deserialize TradingHours from the Json you provided. I guess it is possible with JsonReader but there is an easier way to do it.

Create a class that describes the response - and which wraps everything in the response - as it is, simply:

@Getter
public class HoursResponse {
    private Long licenceNumber; // you can remove licenseNumber if not needed
    private TradingHours[] tradingHours;
    // add also this if needed later
    // private ShutdownHours shutdownHours;
}

Deserialize that class:

HoursResponse hr = gson.fromJson(json, HoursResponse.class);

and access TradingHours by:

TradingHours[] tradingHours = hr.getTradingHours();

Also I suggest you to change the class name TradingHours to TradingHour and TradingHourDetails to TradingHourDetail. Plural is usually used in names referring to lists or so. For example, following makes more sense:

TradingHour[] tradingHours = hr.getTradingHours();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot pirho , I had got the solution , but it was too complex and less readable . This solution works for me . I am new to Java serialization and now have a good understanding of it

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.