我可以使用 GsonConverterFactory 将 JSON 转换为 HashMap 吗?
Can I convert the JSON to HashMap using GsonConverterFactory?
我使用 Retrofit 库获得了这些数据
[
{
"countryId": "1472",
"countryName": "{"ar": "ألمانيا", "default": "Germany"}",
"image": "a304035c3dcb42cd990bb69b2f03e31f.png"
},
{
"countryId": "1473",
"countryName": "{"ar": "إيطاليا", "default": "Italy"}",
"image": "5b3ae479ada846e98309ed978c2707b5.png"
},
{
"countryId": "1474",
"countryName": "{"ar": "هولندا", "default": "Netherlands"}",
"image": "d810f9ab22434b4da08b838e72add09d.png"
},
{
"countryId": "1475",
"countryName": "{"ar": "بولندا", "default": "Poland"}",
"image": "d8c4de2a11ca45759089fec204af9659.png"
},
{
"countryId": "1476",
"countryName": "{"ar": "رومانيا", "default": "Romania"}",
"image": "47efdea8456244a5b9aae7132fca7418.png"
},
{
"countryId": "1477",
"countryName": "{"ar": "روسيا", "default": "Russia"}",
"image": "7163f60a1c494e1b9f782edd3ecabd31.png"
},
{
"countryId": "1478",
"countryName": "{"ar": "إسبانيا", "default": "Spain"}",
"image": "52fe49f594074b078fd5d8c9625018ee.png"
},
{
"countryId": "1479",
"countryName": "{"ar": "اوكرانيا", "default": "Ukraine"}",
"image": "28581f7e4f324d938e0b109f7ee9203e.png"
},
{
"countryId": "1480",
"countryName": "{"ar": "المملكة المتحدة", "default": "United Kingdom"}",
"image": "e7a87ff0caa241559f6c2559cc8606c3.png"
},
{
"countryId": "2147483647",
"countryName": "{"ar": "فرنسا", "default": "France"}",
"image": "3830917201c74fc9b6b4ed0ddfdd4866.png"
}
]
这是获取数据的代码
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.baseURL).addConverterFactory(GsonConverterFactory.create()).build();
TestInterface testInterface = retrofit.create(TestInterface.class);
testInterface.getCountries().enqueue(new Callback < List < CountriesModel >> () {
@Override
public void onResponse(@NonNull Call < List < CountriesModel >> call, @NonNull Response < List < CountriesModel >> response) {
if (response.body() == null)
return;
for (CountriesModel item: response.body()) {
Chip chip = new Chip(requireActivity());
chip.setText(item.getCountryName());
fragmentSelectCountryBinding.fragmentSelectCountryChipGroup281.addView(chip);
}
}
@Override
public void onFailure(@NonNull Call < List < CountriesModel >> call, @NonNull Throwable t) {
Log.w(Tag, "Failed - " + t.getMessage());
}
});
测试界面
public interface TestInterface {
@GET("FetchCountries.php")
Call<List<CountriesModel>> getCountries();
}
国家/地区模型 Class
public class CountriesModel {
@SerializedName("countryId")
private long countryId;
@SerializedName("countryName")
private String countryName;
@SerializedName("image")
private String image;
public long getCountryId() {
return countryId;
}
public String getCountryName() {
return countryName;
}
public String getImage() {
return image;
}
}
MySQL
中的列
芯片中显示的国家名称是这样的{"ar": "ألمانيا", "default": "Germany"}, {"ar": "إيطاليا", "default": "Italy"}, {"ar": "هولندا", "default": "Netherlands"}, etc...
国名里面的JSONwho,可以存到HashMap中,像下面这样根据key取值吗?
chip.setText(item.getCountryName().get("default"));
我可以使用 GsonConverterFactory
做这样的事情吗?
编辑
与我想要的很接近,但我想在同一个模型中做到这一点而不创建两个模型。
也许你可以,但我不确定你是否应该这样做。你的问题好像是国名其实是“inner”JSON。我想它实际上是像下面这样转义的,否则它将无法解析:
[
{
"countryId": "1476",
"countryName": "{\"ar\": \"رومانيا\", \"default\": \"Romania\"}",
"image": "47efdea8456244a5b9aae7132fca7418.png"
}
]
可以只为 countryName 创建一个自己的 class 和一个自定义反序列化器来处理这个“内部”JSON。请参阅下面的示例(不需要是静态内部 classes 但为简洁起见):
@Getter @Setter
public class CountriesModel {
private long countryId;
// define representation for complex name
@Getter @Setter
public static class CountryName {
// define a custom deserializer for "inner" JSON
public static class CountryNameDeserializer
implements JsonDeserializer<CountryName> {
private final Gson gson = new Gson();
@Override
public CountryName deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// gets the field as string and parses it again as a CountryName object
return gson.fromJson(json.getAsString(), CountryName.class);
}
}
private String ar;
// default is a reserved word
@SerializedName("default")
private String defaultName;
}
// Use the adapter
@JsonAdapter(CountryNameDeserializer.class)
private CountryName countryName;
private String image;
}
现在就像:
someCountriesModel.getCountryName().getDefaultName();
我使用 Retrofit 库获得了这些数据
[
{
"countryId": "1472",
"countryName": "{"ar": "ألمانيا", "default": "Germany"}",
"image": "a304035c3dcb42cd990bb69b2f03e31f.png"
},
{
"countryId": "1473",
"countryName": "{"ar": "إيطاليا", "default": "Italy"}",
"image": "5b3ae479ada846e98309ed978c2707b5.png"
},
{
"countryId": "1474",
"countryName": "{"ar": "هولندا", "default": "Netherlands"}",
"image": "d810f9ab22434b4da08b838e72add09d.png"
},
{
"countryId": "1475",
"countryName": "{"ar": "بولندا", "default": "Poland"}",
"image": "d8c4de2a11ca45759089fec204af9659.png"
},
{
"countryId": "1476",
"countryName": "{"ar": "رومانيا", "default": "Romania"}",
"image": "47efdea8456244a5b9aae7132fca7418.png"
},
{
"countryId": "1477",
"countryName": "{"ar": "روسيا", "default": "Russia"}",
"image": "7163f60a1c494e1b9f782edd3ecabd31.png"
},
{
"countryId": "1478",
"countryName": "{"ar": "إسبانيا", "default": "Spain"}",
"image": "52fe49f594074b078fd5d8c9625018ee.png"
},
{
"countryId": "1479",
"countryName": "{"ar": "اوكرانيا", "default": "Ukraine"}",
"image": "28581f7e4f324d938e0b109f7ee9203e.png"
},
{
"countryId": "1480",
"countryName": "{"ar": "المملكة المتحدة", "default": "United Kingdom"}",
"image": "e7a87ff0caa241559f6c2559cc8606c3.png"
},
{
"countryId": "2147483647",
"countryName": "{"ar": "فرنسا", "default": "France"}",
"image": "3830917201c74fc9b6b4ed0ddfdd4866.png"
}
]
这是获取数据的代码
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.baseURL).addConverterFactory(GsonConverterFactory.create()).build();
TestInterface testInterface = retrofit.create(TestInterface.class);
testInterface.getCountries().enqueue(new Callback < List < CountriesModel >> () {
@Override
public void onResponse(@NonNull Call < List < CountriesModel >> call, @NonNull Response < List < CountriesModel >> response) {
if (response.body() == null)
return;
for (CountriesModel item: response.body()) {
Chip chip = new Chip(requireActivity());
chip.setText(item.getCountryName());
fragmentSelectCountryBinding.fragmentSelectCountryChipGroup281.addView(chip);
}
}
@Override
public void onFailure(@NonNull Call < List < CountriesModel >> call, @NonNull Throwable t) {
Log.w(Tag, "Failed - " + t.getMessage());
}
});
测试界面
public interface TestInterface {
@GET("FetchCountries.php")
Call<List<CountriesModel>> getCountries();
}
国家/地区模型 Class
public class CountriesModel {
@SerializedName("countryId")
private long countryId;
@SerializedName("countryName")
private String countryName;
@SerializedName("image")
private String image;
public long getCountryId() {
return countryId;
}
public String getCountryName() {
return countryName;
}
public String getImage() {
return image;
}
}
MySQL
中的列芯片中显示的国家名称是这样的{"ar": "ألمانيا", "default": "Germany"}, {"ar": "إيطاليا", "default": "Italy"}, {"ar": "هولندا", "default": "Netherlands"}, etc...
国名里面的JSONwho,可以存到HashMap中,像下面这样根据key取值吗?
chip.setText(item.getCountryName().get("default"));
我可以使用 GsonConverterFactory
做这样的事情吗?
编辑
也许你可以,但我不确定你是否应该这样做。你的问题好像是国名其实是“inner”JSON。我想它实际上是像下面这样转义的,否则它将无法解析:
[
{
"countryId": "1476",
"countryName": "{\"ar\": \"رومانيا\", \"default\": \"Romania\"}",
"image": "47efdea8456244a5b9aae7132fca7418.png"
}
]
可以只为 countryName 创建一个自己的 class 和一个自定义反序列化器来处理这个“内部”JSON。请参阅下面的示例(不需要是静态内部 classes 但为简洁起见):
@Getter @Setter
public class CountriesModel {
private long countryId;
// define representation for complex name
@Getter @Setter
public static class CountryName {
// define a custom deserializer for "inner" JSON
public static class CountryNameDeserializer
implements JsonDeserializer<CountryName> {
private final Gson gson = new Gson();
@Override
public CountryName deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// gets the field as string and parses it again as a CountryName object
return gson.fromJson(json.getAsString(), CountryName.class);
}
}
private String ar;
// default is a reserved word
@SerializedName("default")
private String defaultName;
}
// Use the adapter
@JsonAdapter(CountryNameDeserializer.class)
private CountryName countryName;
private String image;
}
现在就像:
someCountriesModel.getCountryName().getDefaultName();