如何使用 Gson 从嵌套的 JSON 对象中获取数据
How to get data from nested JSON objects using Gson
我想从 API 和它 returns 嵌套对象中获取县名;
"countries": {
"1": {
"name": "Cyprus",
"nameTurkish": "KKTC",
"nameNative": "Kıbrıs"
},
"2": {
"name": "Turkey",
"nameTurkish": "Türkiye",
"nameNative": "Türkiye"
},
"3": {
"name": "Monaco",
"nameTurkish": "Monako",
"nameNative": "Monaco"
},
等等有200多个国家,每个县都有自己的"NUMBER_ID"。最后我想列出所有 "name" 信息。我想我应该使用 JsonDeserializer 但不幸的是我不能。
整个 JSON 响应可以读作 JSONObject
,其中包含多个元素,您可以遍历这些元素并获取不同的数据。
String jsonResponse = ""; // Put the entire JSON response as a String
JSONObject root = new JSONObject(jsonResponse);
JSONArray rootArray = root.getJSONArray("countries"); // root element of the json respons
for (int i = 0; i < rootArray.length(); i++) {
JSONObject number = rootArray.getJSONObject(i);
String country = number.getString("name"); // Get country name
// Here you can add `country` into a List
}
更新:
but there is no array in my JSON file, all of them are objects, every
country is in an object and every object has its own SerializedName
您可以将它读入 JSONOjbect,而不是使用 JSON 数组,您可以迭代 JSONObject 的长度,如下所示。
try {
JSONObject root = new JSONObject(jsonResponse);
JSONObject countries = root.getJSONObject("countries");
for (int i = 1; i <= countries.length(); i++) {
JSONObject number = countries.getJSONObject(String.valueOf(i));
String country = number.getString("name"); // Get country name
// Here you can add the `country` into a List
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试使用 TypeToken。
Gson gson = new Gson();
List<Country> list = gson.fromJson(gson.toJson(what_you_get_data), new TypeToken<ArrayList<Country>>(){}.getType()); //Country is VO class what you make.
在这里,您可以看到您的数据看起来像 HashMap
,所以我就这样尝试了,您的数据解析成功,没有出现故障:
创建 Pojo 的:
public class Countries {
private HashMap<String, Country> countries;
public HashMap<String, Country> getCountries() { return countries; }
public void setCountries(HashMap<String, Country> countries) { this.countries = countries; }
}
public class Country {
private String name;
private String nameTurkish;
private String nameNative;
public String getName() { return name; }
public void setName(String name) { this.name = name;}
public String getNameTurkish() { return nameTurkish; }
public void setNameTurkish(String nameTurkish) { this.nameTurkish = nameTurkish; }
public String getNameNative() { return nameNative; }
public void setNameNative(String nameNative) { this.nameNative = nameNative; }
}
创建一个 Gson 对象并解析它:
Gson gson = new Gson();
// Countries Object
Type testC = new TypeToken<Countries>(){}.getType();
Countries ob = gson.fromJson(test, testC);
String newData = gson.toJson(ob.getCountries());
System.out.println("New Data: "+newData);
// All country in HashMap
Type country = new TypeToken<HashMap<String, Country>>(){}.getType();
HashMap<String, Country> countryHashMap = gson.fromJson(newData, country);
// Print All HashMap Country
for (Map.Entry<String, Country> set : countryHashMap.entrySet()) {
System.out.println("==> "+set.getKey() + " = " + set.getValue());
}
输出:
I/System.out: ==> 1 = Country{name='Cyprus', nameTurkish='KKTC', nameNative='Kıbrıs'}
I/System.out: ==> 2 = Country{name='Turkey', nameTurkish='Türkiye', nameNative='Türkiye'}
I/System.out: ==> 3 = Country{name='Monaco', nameTurkish='Monako', nameNative='Monaco'}
我想从 API 和它 returns 嵌套对象中获取县名;
"countries": {
"1": {
"name": "Cyprus",
"nameTurkish": "KKTC",
"nameNative": "Kıbrıs"
},
"2": {
"name": "Turkey",
"nameTurkish": "Türkiye",
"nameNative": "Türkiye"
},
"3": {
"name": "Monaco",
"nameTurkish": "Monako",
"nameNative": "Monaco"
},
等等有200多个国家,每个县都有自己的"NUMBER_ID"。最后我想列出所有 "name" 信息。我想我应该使用 JsonDeserializer 但不幸的是我不能。
整个 JSON 响应可以读作 JSONObject
,其中包含多个元素,您可以遍历这些元素并获取不同的数据。
String jsonResponse = ""; // Put the entire JSON response as a String
JSONObject root = new JSONObject(jsonResponse);
JSONArray rootArray = root.getJSONArray("countries"); // root element of the json respons
for (int i = 0; i < rootArray.length(); i++) {
JSONObject number = rootArray.getJSONObject(i);
String country = number.getString("name"); // Get country name
// Here you can add `country` into a List
}
更新:
but there is no array in my JSON file, all of them are objects, every country is in an object and every object has its own SerializedName
您可以将它读入 JSONOjbect,而不是使用 JSON 数组,您可以迭代 JSONObject 的长度,如下所示。
try {
JSONObject root = new JSONObject(jsonResponse);
JSONObject countries = root.getJSONObject("countries");
for (int i = 1; i <= countries.length(); i++) {
JSONObject number = countries.getJSONObject(String.valueOf(i));
String country = number.getString("name"); // Get country name
// Here you can add the `country` into a List
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试使用 TypeToken。
Gson gson = new Gson();
List<Country> list = gson.fromJson(gson.toJson(what_you_get_data), new TypeToken<ArrayList<Country>>(){}.getType()); //Country is VO class what you make.
在这里,您可以看到您的数据看起来像 HashMap
,所以我就这样尝试了,您的数据解析成功,没有出现故障:
创建 Pojo 的:
public class Countries { private HashMap<String, Country> countries; public HashMap<String, Country> getCountries() { return countries; } public void setCountries(HashMap<String, Country> countries) { this.countries = countries; } } public class Country { private String name; private String nameTurkish; private String nameNative; public String getName() { return name; } public void setName(String name) { this.name = name;} public String getNameTurkish() { return nameTurkish; } public void setNameTurkish(String nameTurkish) { this.nameTurkish = nameTurkish; } public String getNameNative() { return nameNative; } public void setNameNative(String nameNative) { this.nameNative = nameNative; } }
创建一个 Gson 对象并解析它:
Gson gson = new Gson(); // Countries Object Type testC = new TypeToken<Countries>(){}.getType(); Countries ob = gson.fromJson(test, testC); String newData = gson.toJson(ob.getCountries()); System.out.println("New Data: "+newData); // All country in HashMap Type country = new TypeToken<HashMap<String, Country>>(){}.getType(); HashMap<String, Country> countryHashMap = gson.fromJson(newData, country); // Print All HashMap Country for (Map.Entry<String, Country> set : countryHashMap.entrySet()) { System.out.println("==> "+set.getKey() + " = " + set.getValue()); }
输出:
I/System.out: ==> 1 = Country{name='Cyprus', nameTurkish='KKTC', nameNative='Kıbrıs'} I/System.out: ==> 2 = Country{name='Turkey', nameTurkish='Türkiye', nameNative='Türkiye'} I/System.out: ==> 3 = Country{name='Monaco', nameTurkish='Monako', nameNative='Monaco'}