Преобразование из json в объект Java
Есть json файл. Подскажите, пожалуйста, как преобразовать в объект с помощью gson
{
"movie": [
{
"name": "Home alone",
"year": 1995,
"description": "Comedy",
"director": {
"fullName": "Testov Test"
},
"cast": [
{
"fullName": "Testov Test",
"role": "some role"
},
{
"fullName": "Testov Test",
"role": "some role"
}
]
},
{
"name": "the ring",
"year": 1990,
"description": "horor",
"director": {
"fullName": "Some Director"
},
"cast": [
{
"fullName": "some actor",
"role": "some role"
},
{
"fullName": "Some Girl",
"role": "some role"
}
]
}
}
Класс:
public class Movie{
private String name;
private int year;
private String description;
// какое должно быть поле для director ?
// какое должно быть поле cast ?
}
Ответы (1 шт):
Автор решения: AVRamones
→ Ссылка
В таких случаях идете на этот прекрасный ресурс: http://www.jsonschema2pojo.org/ Создаете себе POJO для ваших классов :
public class Cast {
@SerializedName("fullName")
@Expose
private String fullName;
@SerializedName("role")
@Expose
private String role;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Director {
@SerializedName("fullName")
@Expose
private String fullName;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Movie {
@SerializedName("name")
@Expose
private String name;@SerializedName("year")
@Expose
private Integer year;
@SerializedName("description")
@Expose
private String description;
@SerializedName("director")
@Expose
private Director director;
@SerializedName("cast")
@Expose
private List<Cast> cast = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Director getDirector() {
return director;
}
public void setDirector(Director director) {
this.director = director;
}
public List<Cast> getCast() {
return cast;
}
public void setCast(List<Cast> cast) {
this.cast = cast;
}
}
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("movie")
@Expose
private List<Movie> movie = null;
public List<Movie> getMovie() {
return movie;
}
public void setMovie(List<Movie> movie) {
this.movie = movie;
}
Ну и потом все просто (надеюсь у Вас добавлены все необходимые зависимости добавляющие Gson в Ваш проект)
Gson gson = new Gson();
Response response = gson.fromJson(yourJson, Response.class);
List<Movie> movies = response.getMovie();