I recommend using the library called Jackson, it is very good to do this kind of conversion into java objects.
If you use maven, you can import the following library:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0.pr3</version>
</dependency>
If you do not use, import the following jar into your application:
http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.9.0.pr3/jackson-databind-2.9.0.pr3.jar
Here is a working example of json reading:
Create a class called Champion with the following data:
import java.util.Map;
public class Champion {
private String type;
private String version;
private Map<Integer, DadosChampion> data;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Map<Integer, DadosChampion> getData() {
return data;
}
public void setData(Map<Integer, DadosChampion> data) {
this.data = data;
}
@Override
public String toString() {
return "Champion [type=" + type + ", version=" + version + ", data=" + data + "]";
}
}
Create another class called DataChampion with the following data:
public class DadosChampion {
private Integer id;
private String key;
private String name;
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Finally, create a last class called Principal with the following data:
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Principal {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"type\":\"champion\",\"version\":\"7.10.1\",\"data\":{\"89\":{\"id\":89,\"key\":\"Leona\",\"name\":\"Leona\",\"title\":\"a Alvorada Radiante\"},\"110\":{\"id\":110,\"key\":\"Varus\",\"name\":\"Varus\",\"title\":\"a Flecha da Vingança\"}}}";
ObjectMapper mapper = new ObjectMapper();
Champion champion = mapper.readValue(json, Champion.class);
System.out.println("Tipo: " + champion.getType());
System.out.println("Versão: " + champion.getVersion());
System.out.println();
System.out.println("Dados do champion:");
System.out.println("---------------");
for (DadosChampion dadosChampion : champion.getData().values()) {
System.out.println("ID: " + dadosChampion.getId());
System.out.println("KEY: " + dadosChampion.getKey());
System.out.println("NOME: " + dadosChampion.getName());
System.out.println("Title: " + dadosChampion.getTitle());
System.out.println("---------------");
}
System.out.println();
System.out.println("OUTRA FORMA DE PERCORRER: ");
for (Map.Entry<Integer, DadosChampion> dadosChampion : champion.getData().entrySet()) {
System.out.println("ID INDICE: " + dadosChampion.getKey());
System.out.println("ID: " + dadosChampion.getValue().getId());
System.out.println("KEY: " + dadosChampion.getValue().getKey());
System.out.println("NOME: " + dadosChampion.getValue().getName());
System.out.println("Title: " + dadosChampion.getValue().getTitle());
System.out.println("---------------");
}
}
}
The output will be as follows:
Tipo: champion
Versão: 7.10.1
Dados do champion:
---------------
ID: 89
KEY: Leona
NOME: Leona
Title: a Alvorada Radiante
---------------
ID: 110
KEY: Varus
NOME: Varus
Title: a Flecha da Vingança
---------------
Hugs.