如何解析 json 数据 Android
How to parse json Data Android
如何将具有相同标题的系列解析为一个数组列表
所以我得到了第 1 季和第 2 季的标题名称
最好的方法是什么
我的Json数据
{<br>
"series":[<br>
{<br>
"title":"Jumping cat",
"genre":"comedy",
"year":2018,
"season":1,
"imdb":7,
"info":"comdey series",
"episodes":10,
"cover":"poster"
},
{<br>
"title":"Jumping cat",
"genre":"comedy",
"year":2019,
"season":2,
"imdb":7,
"info":"comdey series",
"episodes":11,
"cover":"poster"
}
]
}
以下代码将创建一个带有字符串键和 ArrayList 值的“HashMap”。
ArrayList 包含每个系列的模型:
try{
JSONObject reader = new JSONObject(str);
JSONArray array = reader.optJSONArray("series");
HashMap<String, ArrayList<YourModel>> map = new HashMap<>();
for(int i=0;i<array.length();i++){
JSONObject innerObject = array.getJSONObject(i);
if(map.get(innerObject.getString("title")) != null){ // check if the title already exists, then add it to it's list
ArrayList<YourModel> arrayList = map.get(innerObject.getString("title"));
arrayList.add(new YourModel(innerObject));
}else{ // if the title does not exist, create new ArrayList
ArrayList<YourModel> arrayList = new ArrayList<>();
arrayList.add(new YourModel(innerObject));
map.put(innerObject.getString("title"),arrayList);
}
}
}catch (JSONException e){
// Do error handling
}
要获取 Json 的信息,您总是需要两件事
- POJO 的模型 class ob 您将获得的对象
- 选择使用一个或JsonJava原生的解析器或第三方的Gson
希望对您有所帮助:D
您的回复以列表开头
@SerializedName("series")
@Expose
private List<Series> series = null;
List model class
@SerializedName("title")
@Expose
private String title;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("year")
@Expose
private Integer year;
@SerializedName("season")
@Expose
private Integer season;
@SerializedName("imdb")
@Expose
private Integer imdb;
@SerializedName("info")
@Expose
private String info;
@SerializedName("episodes")
@Expose
private Integer episodes;
@SerializedName("cover")
@Expose
private String cover;
And create getter setter method
如果您不想再添加第 3 方。您可以在几行中完成此操作。是的,它是体力劳动,但它会节省大量添加到您的 APK 的字节码。
public class Serie {
public String title;
public String genere;
public int year;
public int season;
public int imdb;
public String info;
public int episodes;
public String cover;
public static Serie toObject(JSONObject o) throws JSONException {
Serie s = new Serie();
s.title = o.getString("title");
s.genere = o.getString("genre");
s.year = o.getInt("year");
s.season = o.getInt("season");
s.imdb = o.getInt("imdb");
s.info = o.getString("info");
s.episodes = o.getInt("episodes");
s.cover = o.getString("cover");
return s;
}
public static List<Serie> toArray(String json) throws JSONException {
JSONObject oo = new JSONObject(json);
JSONArray a = oo.getJSONArray("series");
List<Serie> l = new ArrayList<>(a.length());
for (int i =0; i<a.length(); i++ ) {
JSONObject o = a.getJSONObject(i);
l.add(Serie.toObject(o));
}
return l;
}
}
// usage
try {
List<Serie> ll = Serie.toArray(s);
System.out.println(ll.toString());
} catch (JSONException e) {
e.printStackTrace();
}
您可以使用 Google 的 gson library for simply parse json into java classe and vice versa. An example for how to use gson found here
如何将具有相同标题的系列解析为一个数组列表
所以我得到了第 1 季和第 2 季的标题名称
最好的方法是什么
我的Json数据
{<br>
"series":[<br>
{<br>
"title":"Jumping cat",
"genre":"comedy",
"year":2018,
"season":1,
"imdb":7,
"info":"comdey series",
"episodes":10,
"cover":"poster"
},
{<br>
"title":"Jumping cat",
"genre":"comedy",
"year":2019,
"season":2,
"imdb":7,
"info":"comdey series",
"episodes":11,
"cover":"poster"
}
]
}
以下代码将创建一个带有字符串键和 ArrayList 值的“HashMap”。 ArrayList 包含每个系列的模型:
try{
JSONObject reader = new JSONObject(str);
JSONArray array = reader.optJSONArray("series");
HashMap<String, ArrayList<YourModel>> map = new HashMap<>();
for(int i=0;i<array.length();i++){
JSONObject innerObject = array.getJSONObject(i);
if(map.get(innerObject.getString("title")) != null){ // check if the title already exists, then add it to it's list
ArrayList<YourModel> arrayList = map.get(innerObject.getString("title"));
arrayList.add(new YourModel(innerObject));
}else{ // if the title does not exist, create new ArrayList
ArrayList<YourModel> arrayList = new ArrayList<>();
arrayList.add(new YourModel(innerObject));
map.put(innerObject.getString("title"),arrayList);
}
}
}catch (JSONException e){
// Do error handling
}
要获取 Json 的信息,您总是需要两件事
- POJO 的模型 class ob 您将获得的对象
- 选择使用一个或JsonJava原生的解析器或第三方的Gson
希望对您有所帮助:D
您的回复以列表开头
@SerializedName("series")
@Expose
private List<Series> series = null;
List model class
@SerializedName("title")
@Expose
private String title;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("year")
@Expose
private Integer year;
@SerializedName("season")
@Expose
private Integer season;
@SerializedName("imdb")
@Expose
private Integer imdb;
@SerializedName("info")
@Expose
private String info;
@SerializedName("episodes")
@Expose
private Integer episodes;
@SerializedName("cover")
@Expose
private String cover;
And create getter setter method
如果您不想再添加第 3 方。您可以在几行中完成此操作。是的,它是体力劳动,但它会节省大量添加到您的 APK 的字节码。
public class Serie {
public String title;
public String genere;
public int year;
public int season;
public int imdb;
public String info;
public int episodes;
public String cover;
public static Serie toObject(JSONObject o) throws JSONException {
Serie s = new Serie();
s.title = o.getString("title");
s.genere = o.getString("genre");
s.year = o.getInt("year");
s.season = o.getInt("season");
s.imdb = o.getInt("imdb");
s.info = o.getString("info");
s.episodes = o.getInt("episodes");
s.cover = o.getString("cover");
return s;
}
public static List<Serie> toArray(String json) throws JSONException {
JSONObject oo = new JSONObject(json);
JSONArray a = oo.getJSONArray("series");
List<Serie> l = new ArrayList<>(a.length());
for (int i =0; i<a.length(); i++ ) {
JSONObject o = a.getJSONObject(i);
l.add(Serie.toObject(o));
}
return l;
}
}
// usage
try {
List<Serie> ll = Serie.toArray(s);
System.out.println(ll.toString());
} catch (JSONException e) {
e.printStackTrace();
}
您可以使用 Google 的 gson library for simply parse json into java classe and vice versa. An example for how to use gson found here