We have json
{ "cities": [ { "name": "Pyongyang", "id": "1871859", "country": "North Korea", "countryId": "1873107" }, { "name": "Pune", "id": "1259229", "country": "India", "countryId": "1269750" }, { "name": "Paris", "id": "2988507", "country": "France", "countryId": "3017382" } ] } Using retrofit
private RetrofitClient() { retrofit = new Retrofit.Builder() .baseUrl(Configuration.SERVER_API_URL) .client(getOkHttpClient()) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .addConverterFactory(JacksonConverterFactory.create(getObjectMapper())) .build(); } private ObjectMapper getObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); objectMapper.setVisibilityChecker(objectMapper .getSerializationConfig() .getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT) .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); return objectMapper; } City class itself
@JsonRootName(value = "cities") @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "name", "id", "country", "countryId" }) public class City implements Parcelable{ @JsonProperty("name") private String name; @JsonProperty("id") private String id; @JsonProperty("country") private String country; @JsonProperty("countryId") private String countryId; @JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<String, Object>(); public City() { } @JsonProperty("name") public String getName() { return name; } @JsonProperty("name") public void setName(String name) { this.name = name; } @JsonProperty("id") public String getId() { return id; } @JsonProperty("id") public void setId(String id) { this.id = id; } @JsonProperty("country") public String getCountry() { return country; } @JsonProperty("country") public void setCountry(String country) { this.country = country; } @JsonProperty("countryId") public String getCountryId() { return countryId; } @JsonProperty("countryId") public void setCountryId(String countryId) { this.countryId = countryId; } @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } How can I parse json using object mapper, since it displays all values in "not understood"? ////////////////////////////////////////////////// //////////////////////////
Tell me, and with such a json can you suggest why it does not work?
{"path": "http: //*.com/uploads/world/europe/france/cities/paris/inst/", "logo": "logo_sm.png", "list": [{"id": "757", "name": "AAA", "slug": "apprendre-approfondir-accomplir"}, {"id": "1474", "name": "ACCORD", "slug": "accord-french -language-school "}, {" id ":" 749 "," name ":" Alliance Fran \ u00e7aise "," slug ":" alliance-francaise "}, {" id ":" 1672 "," name " : "CCFS", "slug": "ccfs-cours-de-civilization-francaise-de-la-sorbonne-fle -"}, {"id": "1472", "name": "Ecole Suisse Internationale", "slug": "ecole-suisse-internationale"}, {"id": "1538", "name": "EF", "slug": "ef-international-language-school"}, {"id": "1674", "name": "EFI", "slug": "ecole-privee-de-francais-pour-linternational"}, {"id": "1546", "name": "ELFE", "slug ":" elfe-ecole-de-langue-francaise-pour-etrangers - "}, {" id ":" 1566 "," name ":" Eurocentres language school "," slug ":" eurocentres-language-school " }, {"id": "1653", "name": "France Langue Paris Notre-Dame", "slug": "france-langue-paris-notre-dame"}, {"id": "981", "name": "France Langue Paris Op \ u00e9ra", "slug": "france-langue-paris-opera"}, {"id": "1654", "name": "France Langue Paris Victor Hugo", "slug": "france-langue-paris-victor-hugo"}, {"id": "1537", "name": "L'Etoile "," slug ":" letoile "}, {" id ":" 1514 "," name ":" LSI "," slug ":" lsi-6 "}, {" id ":" 752 "," name ":" Lutece Langue "," slug ":" lutece-langue "}, {" id ":" 1466 "," name ":" L \ u2019Atelier 9 "," slug ":" latelier-9 "}, { "id": "1561", "name": "OISE", "slug": "oise"}, {"id": "1671", "name": "Paris Langues (Member CEI)", "slug" : "paris-langues-member-cei -"}, {"id": "121", "name": "Sprachcaffe Paris", "slug": "sprachcaffe-languages-plus-paris"}, {"id" : "755", "name": "Verlaine Langue", "slug": "verlaine-langue"}, {"id": "756", "name": "Vis- \ u0430-Vis", "slug" : "vis_a_vis"}], "empty": "I'm just a tourist"}
retrofit is the same for everyone
Here are 2 classes (at your help, I did the same here)
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "path", "logo", "list", "empty" }) public class School { @JsonProperty("path") private String path; @JsonProperty("logo") private String logo; @JsonProperty("list") private List<Schools> list = new ArrayList<>(); @JsonProperty("empty") private String empty; @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<String, Object>(); /** * No args constructor for use in serialization * */ public School() { } /** * * @param logo * @param empty * @param path * @param list */ public School(String path, String logo, List<Schools> list, String empty) { this.path = path; this.logo = logo; this.list = list; this.empty = empty; } /** * * @return * The path */ @JsonProperty("path") public String getPath() { return path; } /** * * @param path * The path */ @JsonProperty("path") public void setPath(String path) { this.path = path; } /** * * @return * The logo */ @JsonProperty("logo") public String getLogo() { return logo; } /** * * @param logo * The logo */ @JsonProperty("logo") public void setLogo(String logo) { this.logo = logo; } /** * * @return * The list */ @JsonProperty("list") public List<Schools> getList() { return list; } /** * * @param list * The list */ @JsonProperty("list") public void setList(List<Schools> list) { this.list = list; } /** * * @return * The empty */ @JsonProperty("empty") public String getEmpty() { return empty; } /** * * @param empty * The empty */ @JsonProperty("empty") public void setEmpty(String empty) { this.empty = empty; } @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } } @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "id", "name", "slug" }) public class Schools { @JsonProperty("id") private String id; @JsonProperty("name") private String name; @JsonProperty("slug") private String slug; @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<String, Object>(); /** * No args constructor for use in serialization * */ public Schools() { } /** * * @param id * @param name * @param slug */ public Schools(String id, String name, String slug) { this.id = id; this.name = name; this.slug = slug; } /** * * @return * The id */ @JsonProperty("id") public String getId() { return id; } /** * * @param id * The id */ @JsonProperty("id") public void setId(String id) { this.id = id; } /** * * @return * The name */ @JsonProperty("name") public String getName() { return name; } /** * * @param name * The name */ @JsonProperty("name") public void setName(String name) { this.name = name; } /** * * @return * The slug */ @JsonProperty("slug") public String getSlug() { return slug; } /** * * @param slug * The slug */ @JsonProperty("slug") public void setSlug(String slug) { this.slug = slug; } @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } //region ==================== toString() ==================== @Override public String toString() { return name; } //endregion } The request looks like this
@GET("/schools/city/{id}") Observable<School> getSchoolList(@Path("id") String id);