我在使用JACKSON进行JSON解析时非常新,关于我想要实现的目标很少 - 我必须解析JSON值并收集它以将收集的值与我使用selenium检索的Live内容进行比较,我使用genson为json创建对象类。
我的节目的JsonGen.java -
import java.util.List;
public class JsonGen{
private String _type;
private List cast;
private List clips;
private Common_sense_data common_sense_data;
private String common_sense_id;
private List crew;
private String description;
private List episodes;
private Number franchise_id;
private List genres;
private String guid;
private Images images;
private boolean is_locked;
private boolean is_mobile;
private boolean is_parental_locked;
private String kind;
private String mobile_networks;
private String most_recent_full_episode_added_date;
private String name;
private List networks;
private String platforms;
private List ratings;
private String release_date;
private List season_filters;
private String slug;
private String tms_id;
import java.util.List;
public class Cast{
private String first_name;
private String last_name;
private String middle_name;
public String getFirst_name(){
return this.first_name;
}
public void setFirst_name(String first_name){
this.first_name = first_name;
}
public String getLast_name(){
return this.last_name;
}
public void setLast_name(String last_name){
this.last_name = last_name;
}
public String getMiddle_name(){
return this.middle_name;
}
public void setMiddle_name(String middle_name){
this.middle_name = middle_name;
}
}
import java.util.List;
public class Clips{
private String description;
private boolean is_locked;
private boolean is_parental_locked;
private Number run_time;
private String slug;
private List streams;
private String thumbnail_url;
private String title;
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public Number getRun_time(){
return this.run_time;
}
public void setRun_time(Number run_time){
this.run_time = run_time;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public List getStreams(){
return this.streams;
}
public void setStreams(List streams){
this.streams = streams;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getTitle(){
return this.title;
}
public void setTitle(String title){
this.title = title;
}
}
import java.util.List;
public class Common_sense_data{
private Slider slider;
public Slider getSlider(){
return this.slider;
}
public void setSlider(Slider slider){
this.slider = slider;
}
}
import java.util.List;
public class Crew{
private List members;
private String role;
public List getMembers(){
return this.members;
}
public void setMembers(List members){
this.members = members;
}
public String getRole(){
return this.role;
}
public void setRole(String role){
this.role = role;
}
}
import java.util.List;
public class Episodes{
private String date_dead;
private String description;
private String episode_id;
private String episode_name;
private String episode_number;
private String franchise_description;
private String franchise_id;
private String franchise_name;
private String franchise_slug;
private String guid;
private String id_from_partner;
private boolean is_locked;
private String is_mobile;
private boolean is_parental_locked;
private boolean is_subscription;
private String media_base_url;
private String metadata_url;
private String mobile_streams;
private String network;
private String network_analytics;
private String network_id;
private boolean network_is_dand_enabled;
private String network_logo;
private String network_name;
private String network_slug;
private String original_air_date;
private String platforms;
private String player_type;
private String playlist_url;
private String poster_url;
private String rating;
private String run_time;
private String season_number;
private String season_sort_key;
private String slug;
private String ssid;
private String thumbnail_url;
private String tms_id;
private String url;
private String video_guid;
private String video_id;
private String wide_poster_url;
public String getDate_dead(){
return this.date_dead;
}
public void setDate_dead(String date_dead){
this.date_dead = date_dead;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public String getEpisode_id(){
return this.episode_id;
}
public void setEpisode_id(String episode_id){
this.episode_id = episode_id;
}
public String getEpisode_name(){
return this.episode_name;
}
public void setEpisode_name(String episode_name){
this.episode_name = episode_name;
}
public String getEpisode_number(){
return this.episode_number;
}
public void setEpisode_number(String episode_number){
this.episode_number = episode_number;
}
public String getFranchise_description(){
return this.franchise_description;
}
public void setFranchise_description(String franchise_description){
this.franchise_description = franchise_description;
}
public String getFranchise_id(){
return this.franchise_id;
}
public void setFranchise_id(String franchise_id){
this.franchise_id = franchise_id;
}
public String getFranchise_name(){
return this.franchise_name;
}
public void setFranchise_name(String franchise_name){
this.franchise_name = franchise_name;
}
public String getFranchise_slug(){
return this.franchise_slug;
}
public void setFranchise_slug(String franchise_slug){
this.franchise_slug = franchise_slug;
}
public String getGuid(){
return this.guid;
}
public void setGuid(String guid){
this.guid = guid;
}
public String getId_from_partner(){
return this.id_from_partner;
}
public void setId_from_partner(String id_from_partner){
this.id_from_partner = id_from_partner;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public String getIs_mobile(){
return this.is_mobile;
}
public void setIs_mobile(String is_mobile){
this.is_mobile = is_mobile;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public boolean getIs_subscription(){
return this.is_subscription;
}
public void setIs_subscription(boolean is_subscription){
this.is_subscription = is_subscription;
}
public String getMedia_base_url(){
return this.media_base_url;
}
public void setMedia_base_url(String media_base_url){
this.media_base_url = media_base_url;
}
public String getMetadata_url(){
return this.metadata_url;
}
public void setMetadata_url(String metadata_url){
this.metadata_url = metadata_url;
}
public String getMobile_streams(){
return this.mobile_streams;
}
public void setMobile_streams(String mobile_streams){
this.mobile_streams = mobile_streams;
}
public String getNetwork(){
return this.network;
}
public void setNetwork(String network){
this.network = network;
}
public String getNetwork_analytics(){
return this.network_analytics;
}
public void setNetwork_analytics(String network_analytics){
this.network_analytics = network_analytics;
}
public String getNetwork_id(){
return this.network_id;
}
public void setNetwork_id(String network_id){
this.network_id = network_id;
}
public boolean getNetwork_is_dand_enabled(){
return this.network_is_dand_enabled;
}
public void setNetwork_is_dand_enabled(boolean network_is_dand_enabled){
this.network_is_dand_enabled = network_is_dand_enabled;
}
public String getNetwork_logo(){
return this.network_logo;
}
public void setNetwork_logo(String network_logo){
this.network_logo = network_logo;
}
public String getNetwork_name(){
return this.network_name;
}
public void setNetwork_name(String network_name){
this.network_name = network_name;
}
public String getNetwork_slug(){
return this.network_slug;
}
public void setNetwork_slug(String network_slug){
this.network_slug = network_slug;
}
public String getOriginal_air_date(){
return this.original_air_date;
}
public void setOriginal_air_date(String original_air_date){
this.original_air_date = original_air_date;
}
public String getPlatforms(){
return this.platforms;
}
public void setPlatforms(String platforms){
this.platforms = platforms;
}
public String getPlayer_type(){
return this.player_type;
}
public void setPlayer_type(String player_type){
this.player_type = player_type;
}
public String getPlaylist_url(){
return this.playlist_url;
}
public void setPlaylist_url(String playlist_url){
this.playlist_url = playlist_url;
}
public String getPoster_url(){
return this.poster_url;
}
public void setPoster_url(String poster_url){
this.poster_url = poster_url;
}
public String getRating(){
return this.rating;
}
public void setRating(String rating){
this.rating = rating;
}
public String getRun_time(){
return this.run_time;
}
public void setRun_time(String run_time){
this.run_time = run_time;
}
public String getSeason_number(){
return this.season_number;
}
public void setSeason_number(String season_number){
this.season_number = season_number;
}
public String getSeason_sort_key(){
return this.season_sort_key;
}
public void setSeason_sort_key(String season_sort_key){
this.season_sort_key = season_sort_key;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getSsid(){
return this.ssid;
}
public void setSsid(String ssid){
this.ssid = ssid;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getTms_id(){
return this.tms_id;
}
public void setTms_id(String tms_id){
this.tms_id = tms_id;
}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
public String getVideo_guid(){
return this.video_guid;
}
public void setVideo_guid(String video_guid){
this.video_guid = video_guid;
}
public String getVideo_id(){
return this.video_id;
}
public void setVideo_id(String video_id){
this.video_id = video_id;
}
public String getWide_poster_url(){
return this.wide_poster_url;
}
public void setWide_poster_url(String wide_poster_url){
this.wide_poster_url = wide_poster_url;
}
}
import java.util.List;
public class Genres{
private String name;
private String slug;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
}
import java.util.List;
public class Images{
private String poster_url;
private String thumbnail_url;
private String wide_poster_url;
public String getPoster_url(){
return this.poster_url;
}
public void setPoster_url(String poster_url){
this.poster_url = poster_url;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getWide_poster_url(){
return this.wide_poster_url;
}
public void setWide_poster_url(String wide_poster_url){
this.wide_poster_url = wide_poster_url;
}
}
import java.util.List;
public class Networks{
private String banner;
private String description;
private boolean is_locked;
private String logo;
private String name;
private String network_analytics;
private Number network_id;
private String slug;
private String thumbnail_url;
private String url;
public String getBanner(){
return this.banner;
}
public void setBanner(String banner){
this.banner = banner;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public String getLogo(){
return this.logo;
}
public void setLogo(String logo){
this.logo = logo;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getNetwork_analytics(){
return this.network_analytics;
}
public void setNetwork_analytics(String network_analytics){
this.network_analytics = network_analytics;
}
public Number getNetwork_id(){
return this.network_id;
}
public void setNetwork_id(Number network_id){
this.network_id = network_id;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
}
import java.util.List;
public class Ratings{
private String description;
private String rating;
private String rating_body;
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public String getRating(){
return this.rating;
}
public void setRating(String rating){
this.rating = rating;
}
public String getRating_body(){
return this.rating_body;
}
public void setRating_body(String rating_body){
this.rating_body = rating_body;
}
}
import java.util.List;
public class Season_filters{
private Number episode_count;
private String name;
private String slug;
public Number getEpisode_count(){
return this.episode_count;
}
public void setEpisode_count(Number episode_count){
this.episode_count = episode_count;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
}
import java.util.List;
public class Slider{
private Number not_for_kids;
private Number off_age;
private Number on_age;
private String status;
private Number target_age;
private String text;
public Number getNot_for_kids(){
return this.not_for_kids;
}
public void setNot_for_kids(Number not_for_kids){
this.not_for_kids = not_for_kids;
}
public Number getOff_age(){
return this.off_age;
}
public void setOff_age(Number off_age){
this.off_age = off_age;
}
public Number getOn_age(){
return this.on_age;
}
public void setOn_age(Number on_age){
this.on_age = on_age;
}
public String getStatus(){
return this.status;
}
public void setStatus(String status){
this.status = status;
}
public Number getTarget_age(){
return this.target_age;
}
public void setTarget_age(Number target_age){
this.target_age = target_age;
}
public String getText(){
return this.text;
}
public void setText(String text){
this.text = text;
}
}
import java.util.List;
public class Streams{
private String date_dead;
private String franchise_name;
private String id_from_partner;
private boolean is_locked;
private boolean is_parental_locked;
private boolean is_subscription;
private String media_base_url;
private Number mediaview_partner_id;
private Number mediaview_provider_id;
private String metadata_url;
private String network;
private String network_analytics;
private String network_id;
private boolean network_is_dand_enabled;
private String network_name;
private String network_slug;
private String player_type;
private String playlist_url;
private String rating;
private Number run_time;
private Number selection_score;
private String ssid;
private String thumbnail_url;
private String uma_supported;
private String url;
private String video_guid;
private String video_id;
public String getDate_dead(){
return this.date_dead;
}
public void setDate_dead(String date_dead){
this.date_dead = date_dead;
}
public String getFranchise_name(){
return this.franchise_name;
}
public void setFranchise_name(String franchise_name){
this.franchise_name = franchise_name;
}
public String getId_from_partner(){
return this.id_from_partner;
}
public void setId_from_partner(String id_from_partner){
this.id_from_partner = id_from_partner;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public boolean getIs_subscription(){
return this.is_subscription;
}
public void setIs_subscription(boolean is_subscription){
this.is_subscription = is_subscription;
}
public String getMedia_base_url(){
return this.media_base_url;
}
public void setMedia_base_url(String media_base_url){
this.media_base_url = media_base_url;
}
public Number getMediaview_partner_id(){
return this.mediaview_partner_id;
}
public void setMediaview_partner_id(Number mediaview_partner_id){
this.mediaview_partner_id = mediaview_partner_id;
}
public Number getMediaview_provider_id(){
return this.mediaview_provider_id;
}
public void setMediaview_provider_id(Number mediaview_provider_id){
this.mediaview_provider_id = mediaview_provider_id;
}
public String getMetadata_url(){
return this.metadata_url;
}
public void setMetadata_url(String metadata_url){
this.metadata_url = metadata_url;
}
public String getNetwork(){
return this.network;
}
public void setNetwork(String network){
this.network = network;
}
public String getNetwork_analytics(){
return this.network_analytics;
}
public void setNetwork_analytics(String network_analytics){
this.network_analytics = network_analytics;
}
public String getNetwork_id(){
return this.network_id;
}
public void setNetwork_id(String network_id){
this.network_id = network_id;
}
public boolean getNetwork_is_dand_enabled(){
return this.network_is_dand_enabled;
}
public void setNetwork_is_dand_enabled(boolean network_is_dand_enabled){
this.network_is_dand_enabled = network_is_dand_enabled;
}
public String getNetwork_name(){
return this.network_name;
}
public void setNetwork_name(String network_name){
this.network_name = network_name;
}
public String getNetwork_slug(){
return this.network_slug;
}
public void setNetwork_slug(String network_slug){
this.network_slug = network_slug;
}
public String getPlayer_type(){
return this.player_type;
}
public void setPlayer_type(String player_type){
this.player_type = player_type;
}
public String getPlaylist_url(){
return this.playlist_url;
}
public void setPlaylist_url(String playlist_url){
this.playlist_url = playlist_url;
}
public String getRating(){
return this.rating;
}
public void setRating(String rating){
this.rating = rating;
}
public Number getRun_time(){
return this.run_time;
}
public void setRun_time(Number run_time){
this.run_time = run_time;
}
public Number getSelection_score(){
return this.selection_score;
}
public void setSelection_score(Number selection_score){
this.selection_score = selection_score;
}
public String getSsid(){
return this.ssid;
}
public void setSsid(String ssid){
this.ssid = ssid;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getUma_supported(){
return this.uma_supported;
}
public void setUma_supported(String uma_supported){
this.uma_supported = uma_supported;
}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
public String getVideo_guid(){
return this.video_guid;
}
public void setVideo_guid(String video_guid){
this.video_guid = video_guid;
}
public String getVideo_id(){
return this.video_id;
}
public void setVideo_id(String video_id){
this.video_id = video_id;
}
}
and following is the driver I am using to get the name of the shows-
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.fasterxml.jackson.*;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class BossMan {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
URL jsonUrl = new URL("http://www.dishanywhere.com/radish/v20/dol/shows/carousels/featured.json");
ObjectMapper objmapper = new ObjectMapper();
JsonGen jsongen = null;
jsongen = objmapper.readValue(jsonUrl, JsonGen.class);
System.out.println(jsongen.getName());
}
}
但我面临以下错误 -
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of JsonGen out of START_ARRAY token
at [Source: http://www.dishanywhere.com/radish/v20/dol/shows/carousels/featured.json; line: 1, column: 1]
I need the result in a array but code is failing above exception.
Thanks in advance.
答案 0 :(得分:0)
如果从服务器收到的数据是数组,则需要将List.class作为第二个参数传递给readValue():
List<JsonGen> values = objmapper.readValue(jsonUril, List.class);
答案 1 :(得分:0)
您没有向我们展示JsonGen课程。如果要反序列化的json文件是JsonGen类型的对象数组,那么您需要这样做:
List<JsonGen> jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});
for(JsonGen element : jsongen) {
System.out.println(element.getName())
}