Question

I am trying get a list object from json file usiong gson. It returning list with objects but all attributes are null. How to get the objects properly?

json file:

[{"PeriodEndP":"2014-04-06T00:00:00","SiteKeyP":"00035"},{"PeriodEndP":"2014-04-06T00:00:00","SiteKeyP":"00035"}]

ScheduleDTO.java

public class ScheduleDTO {
String periodEndP;
String siteKeyP;
}

GsonEx.java

public class GsonEx {
  public static void main(String[] args) {
    try
    {
       JsonReader jsonReader = new JsonReader(new FileReader("F:/schedule.txt"));
       Gson gson = new Gson();
      Type ScheduleMsgDestType = new TypeToken<List<ScheduleDTO>>(){}.getType();
  List<ScheduleDTO> ScheduleList = gson.fromJson(jsonReader, ScheduleMsgDestType);
      for(ScheduleDTO t :ScheduleList )
      {
          System.out.println(t.periodEndP);
      }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

Was it helpful?

Solution

Make PeriodEndP to periodEndP

and same for SiteKeyP which will be siteKeyP

The names should be the same in the json and code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top