How to Get a json list of a JAVA Api?

Asked

Viewed 1,419 times

0

I have the following return of an api {"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"}

I’m starting to consume now but I’m not getting it. I researched about how to get this kind of list but still I’m not succeeding, it’s a little strange because it has an array inside the other. Can you give me a hint of how I can proceed?

  • 2

    https://answall.com/a/6050/28595

  • Is this already the return of the API? Already made the request and got this result?

  • That’s the result already

  • Can add your question like you do the request?

  • @Jeffersonschmitt, to make the meaning of words a little more accurate : a json object (like the one you sent) is a value key mapping. A list is a collection of values, such as ['a', 'b', 'c']

1 answer

2


I recommend using the library called Jackson, it is very good to do this type of conversion to 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 not, 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 functional example of the 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 Dadoschampion 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 Main 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.

  • 1

    Thanks even diego , worked right , a single remark is that I had to download the jacksonall because it gave this error: "error: cannot access Jsonprocessingexception" searching the gringo stack explained the people who had this same error used all and it worked , with me also then I will leave the link here also http://www.java2s.com/Code/Jar/j/Downloadjacksonall199jar.htm

  • @Jeffersonschmitt Okay.. Thanks for the feedback, I changed the link to that jar. An observation, if you want to change the name of your java class attributes, you can. Just put the property @Jsonproperty("label") above its attributes and specify which json property it refers to. More details on https://github.com/FasterXML/jackson-docs Att

  • A doubt what would this toString() be? , If I wanted to do it with another json that has more of this same date type for example a type has the "type":,"version": then there is a list called "basic:{...}" and then the "data:{...}" I would add another map? how could I do ?

  • toString returns a representation of the object in the form of a String. If you run the toString method it will not show the reference of the object but its content in the form of String. Depending on the json structure, you would create a Basic class and within it create the date. Whether it will be a Map or not depends on the json structure. After vc creates a Basic class object within the Champion class. If you have an example of json that you are in doubt put here that I show you with examples.

  • I made a Pastebin by exemplifying Pastebin.com/wSrwMkGV has several inside each other, how would it look? Example I have there a "basic" after the image , Stats ,gold,Rune and last the date, I need to make a map of all of them ? where I only need the date. how can I do ?

  • @Jeffersonschmitt Above the class statement you can add the @Jsonignoreproperties property (ignoreUnknown = true) that it will ignore fields that are not in your class and are in json. &#XXqN9K https://pastebin.com/BXXXqN9K Data Classhave: https://pastebin.com/wwxPujLB Main Class: https://pastebin.com/FYZDH5H .

  • I had commented on @jsonignore but did not send it here but these other properties I had not done , it worked all quiet , thanks even for the help , I would not have succeeded without this strength. Hugs

  • @Jeffersonschmitt .

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.