简体   繁体   中英

How wrap json array to custom object with collection via Gson?

How wrap json array to custom object with collection containing this array via Gson? I have following json string:

   [
      {
         "showId":410,
         "siteId":85,
         "name":"Майстер і маргарита",
         "duration":7200,
         "providerId":1016,
         "events":[
            {
               "siteId":85,
               "eventSiteId":0,
               "providerId":1016,
               "eventId":1178,
               "hallId":0,
               "premiere":false,
               "origin":"20140912190000"
            }
         ]
      }
   ]

and want to deserialize it to the object bellow:

public class Shows {

    private List<Show> shows;

    public List<Show> getShows() {
        return shows;
    }

    public void setShows(List<Show> shows) {
        this.shows = shows;
    }

}

You can try this.

//lets assume the json string to be in the variable data
Shows shows = new Gson().fromJson(data, Shows.class);

This Json message is represent a List<Show> that Show contains a List of Events as well.

This is not a Json of Shows , if so it should be like this.

{
 "shows":[
    {
        "showId":410,
        "siteId":85,
        "name":"Майстер і маргарита",
        "duration":7200,
        "providerId":1016,
        "events":[
            {
                "siteId":85,
                "eventSiteId":0,
                "providerId":1016,
                "eventId":1178,
                "hallId":0,
                "premiere":false,
                "origin":"20140912190000"
            }
        ]
    }
 ]

}

But you can try this way to get List<Show> and set it to Shows

You can try this way.

Type collectionType = new TypeToken<List<Show>>() {
    }.getType();
    String jsonString="[\n" +
            "\n" +
            "    {\n" +
            "        \"showId\":410,\n" +
            "        \"siteId\":85,\n" +
            "        \"name\":\"Майстер і маргарита\",\n" +
            "        \"duration\":7200,\n" +
            "        \"providerId\":1016,\n" +
            "        \"events\":[\n" +
            "            {\n" +
            "                \"siteId\":85,\n" +
            "                \"eventSiteId\":0,\n" +
            "                \"providerId\":1016,\n" +
            "                \"eventId\":1178,\n" +
            "                \"hallId\":0,\n" +
            "                \"premiere\":false,\n" +
            "                \"origin\":\"20140912190000\"\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "\n" +
            "]";
    List<Show> showList=new Gson().fromJson(jsonString,collectionType);
    Shows shows=new Shows();
    shows.setShows(showList);
    System.out.println(shows);

My result.

Shows{shows=[Show{showId=410, siteId=85, name='Майстер і маргарита', duration=7200, providerId=1016, events=[Events{siteId=85, eventSiteId=0, providerId=1016, eventId=1178, hallId=0, premiere=false, origin='20140912190000'}]}]}

My Show class.

public class Show {
private int showId;
private int siteId;
private String name;
private int duration;
private int providerId;
private List<Events> events;

//getters and setters

@Override
public String toString() {
    return "Show{" +
            "showId=" + showId +
            ", siteId=" + siteId +
            ", name='" + name + '\'' +
            ", duration=" + duration +
            ", providerId=" + providerId +
            ", events=" + events +
            '}';
     }
}

My Events class

 public class Events {
private int siteId;
private int eventSiteId;
private int providerId;
private int eventId;
private int hallId;
private boolean premiere;
private String origin;

// getters and setters

@Override
public String toString() {
    return "Events{" +
            "siteId=" + siteId +
            ", eventSiteId=" + eventSiteId +
            ", providerId=" + providerId +
            ", eventId=" + eventId +
            ", hallId=" + hallId +
            ", premiere=" + premiere +
            ", origin='" + origin + '\'' +
            '}';
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM