How can I get the data of the following json in java?

0

You see, I have the following json:

"signaturesType":[
            "SIMPLE"
         ],
         "docs":[
            "contrato"
         ]

How can I get the values inside the tags signaturesType and docs. What is strange about me is that it does not come as the classic json, which would be property: value, that is why I doubt how I can obtain the values stored there. Greetings and thanks.

    
asked by 5frags 17.10.2018 в 17:37
source

2 answers

3

You can use Google Gson , but first you should modify your json since you need the {} to indicate that what is inside is an object, you can check all that here JSON org , the brackets [] in JSON represent an array and can be mapped to a Collection as a List or a Array . . p>

import java.util.List;

import com.google.gson.Gson;

public class Test {

    public static void main(String... args) throws Exception {

        String json = "{'signaturesType':["+
                          "'SIMPLE'"+
                       "],"+
                       "'docs':["+
                          "'contrato'"+
                       "]}";

        // Aquí se hace la conversión
        Data data = new Gson().fromJson(json, Data.class);

        // Se muestra así
        System.out.println(data.getSignaturesType().get(0));

        // Se muestra así
        System.out.println(data.getDocs().get(0));
    }

}

class Data {
    private List<String> signaturesType;
    private List<String> docs;
    /**
     * @return the signaturesType
     */
    public List<String> getSignaturesType() {
        return signaturesType;
    }
    /**
     * @param signaturesType the signaturesType to set
     */
    public void setSignaturesType(List<String> signaturesType) {
        this.signaturesType = signaturesType;
    }
    /**
     * @return the docs
     */
    public List<String> getDocs() {
        return docs;
    }
    /**
     * @param docs the docs to set
     */
    public void setDocs(List<String> docs) {
        this.docs = docs;
    }
}

This would be the result:

SIMPLE

contract

    
answered by 17.10.2018 / 18:40
source
-1

To be well formed, the json object should be:

{"signaturesType":["SIMPLE"],"docs":["contrato"]}

What is equivalent to a java class

public class Xxx{
    public List<String> signaturesType;
    public List<String> docs;
}

Using the gson library you can convert the String into an instance of a java class.

String jsonString = "{\"signaturesType\":[\"SIMPLE\"],\"docs\":[\"contrato\"]}";
Xxx xxx = new Gson().fromJson(jsonString, Xxx.class);
    
answered by 17.10.2018 в 18:39