Страницы

Поиск по вопросам

суббота, 13 июля 2019 г.

Json array + object mapper

Имеем 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" } ] }
С помощью 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
@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 additionalProperties = new HashMap(); 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 getAdditionalProperties() { return this.additionalProperties; }
@JsonAnySetter public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); }
Как мне с помощью object mapper правильно парсить json, так как он выводит все значения в "не понимаемые"? ////////////////////////////////////////////////////////////////////////////
Скажите, а с таким json можете подсказать, почему не работает?
{"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-civilisation-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 для всех одинаковый
Вот 2 класса (по Вашей подсказке, сделал аналогично и тут)
@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 list = new ArrayList<>(); @JsonProperty("empty") private String empty; @JsonIgnore private Map additionalProperties = new HashMap();
/** * No args constructor for use in serialization * */ public School() { }
/** * * @param logo * @param empty * @param path * @param list */ public School(String path, String logo, List 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 getList() { return list; }
/** * * @param list * The list */ @JsonProperty("list") public void setList(List 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 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 additionalProperties = new HashMap();
/** * 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 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
}
Запрос выглядит так
@GET("/schools/city/{id}") Observable getSchoolList(@Path("id") String id);


Ответ

Если ретрофиту отдать этот класс будет парсить.
public class Cities {
@JsonProperty("cities") private ArrayList cities;
public ArrayList getCities() { return cities; }
public void setCities(ArrayList cities) { this.cities = cities; } }

Комментариев нет:

Отправить комментарий