Create POJO for JSON

-1

I have a problem creating a POJO of this Json (end of question).

Classes > > > >

Main

public class teste {


public static void main(String[] args) {

    ((Runnable) () -> {
        System.out.println("PreExecute");
        getNetflix("");
    }).run();
}

public static Title getNetflix(String title) {

    final String url = "https://unogs-unogs-v1.p.mashape.com/api.cgi?t=loadvideo&q=60029591";
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(url)
            .addHeader("X-Mashape-Key", "API_KEY")
            .addHeader("Accept", "application/json")
            .build();

    try {
        Response response = client.newCall(request).execute();
        //System.out.println(response.body().string());
         return parseJSONNetflix(response);

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println(e.getMessage().toString());
        System.out.println("NAME ");
    }
    return null;
}

public static Title parseJSONNetflix(Response response) {
    Gson gson = new GsonBuilder().create();
    Title netflix = null;
    try {
        netflix = gson.fromJson(response.body().string(), Title.class);

    } catch (Exception e){
        System.out.println("erro");
        System.out.println(e.toString());
    }
    System.out.println(netflix);
    for (String s : netflix.getRESULT().getNfinfo()) {
            System.out.println(s);

    }
    return netflix;
}


}

Title

 @Generated("com.robohorse.robopojogenerator")
  public class Title{

@SerializedName("RESULT")
@Expose
private RESULT result;

public void setRESULT(RESULT result){
    this.result = result;
}

public RESULT getRESULT(){
    return result;
}
}

People

@Generated("com.robohorse.robopojogenerator")
public class PeopleItem{

@SerializedName("actor")
@Expose
private List<String> actor;

@SerializedName("creator")
@Expose
private List<String> creator = null;

public List<String> getDirector() {
    return director;
}

public void setDirector(List<String> director) {
    this.director = director;
}

@SerializedName("director")
@Expose
private List<String> director = null;

public void setActor(List<String> actor){
    this.actor = actor;
}

public List<String> getActor(){
    return actor;
}
}

Result

@Generated("com.robohorse.robopojogenerator")
public class RESULT{

@SerializedName("country")
@Expose
private List<List<String>> country;

@SerializedName("nfinfo")
@Expose
private List<String> nfinfo;

@SerializedName("Genreid")
@Expose
private List<String> genreid;

@SerializedName("mgname")
@Expose
private List<String> mgname;

@SerializedName("imdbinfo")
@Expose
private List<String> imdbinfo;

@SerializedName("people")
@Expose
private List<PeopleItem> people;




public void setNfinfo(List<String> nfinfo){
    this.nfinfo = nfinfo;
}

public List<String> getNfinfo(){
    return nfinfo;
}

public void setGenreid(List<String> genreid){
    this.genreid = genreid;
}

public List<String> getGenreid(){
    return genreid;
}

public void setMgname(List<String> mgname){
    this.mgname = mgname;
}

public List<String> getMgname(){
    return mgname;
}

public void setImdbinfo(List<String> imdbinfo){
    this.imdbinfo = imdbinfo;
}

public List<String> getImdbinfo(){
    return imdbinfo;
}

public void setPeople(List<PeopleItem> people){
    this.people = people;
}

public List<PeopleItem> getPeople(){
    return people;
}
}

JSON

{
"RESULT": {
    "nfinfo": [
        "https://scdn.nflximg.net/ipl/52911/91a8a4ec55518f9d4e42a5441b67cb732e9b657a.jpg",
        "Out for a Kill",
        "In China, Robert Burns digs up a treasure trove of artifacts and discovers they&#39;re the kind the Chinese mafia use to spirit drugs out of the country.",
        "",
        "General Audiences",
        "3.2870402",
        "movie",
        "2017-01-06 03:43:43",
        "2016-02-05 19:35:52",
        "2003",
        "60029591",
        "1h30m",
        "https://scdn.nflximg.net/ipl/52911/91a8a4ec55518f9d4e42a5441b67cb732e9b657a.jpg",
        "1"
    ],
    "imdbinfo": [
        "3.3",
        "5193",
        "N/A",
        "Action, Crime, Thriller",
        "N/A",
        "90 min",
        "Archeology Professor Robert Burns is on location leading an important dig in the ancient ruins on the Far Eastern Chinese border. He accidentally discovers that the Chinese Mafia, the Tong, is using his newly discovered ancient Chinese artifacts to hide and smuggle narcotics across the border. Robert immediately tries to flee with his assistant and narrowly manages to escape the pursuing Tong but not without a heavy price. His loyal assistant is killed and he is framed with the evidence at the Chinese border by the Chinese military and U.S. Drug Enforcement Agency. He is locked up in a Chinese prison where he is guilty until proven innocent. His loving wife Maya vows to help get him out of jail. The DEA finally convinces the Chinese military that Robert may be of more help to them outside jail by leading them to the real smugglers. Once he leaves his Chinese prison cell Robert would rather enjoy his freedom back in the U.S. and keep his past behind him, but The Tong catches up to him and threatens not only his life but that of his wife. They threatened the wrong man. Secrets of Robert&#39;s past start to emerge as he unleashes his awesome martial arts fighting abilities to defend himself and his wife. Now that The Tong and their drug cartel know who he is they will stop at nothing to kill Robert to protect their own corrupt interests. To avenge the lives of loved ones he has lost, Robert must hunt down, pursue and find each of the Tong members. In a classic homage to Chinese martial arts films, he is involved in intense hand to hand combat including sword fighting, guns, car chases, and other ingenious forms of fighting to mame and kill these Tong members. He deciphers their ancient coded Tong tattoos which leads him to their don, Wong Dai- The Emperor; the man who controls an empire of crime and corruption across many borders. As an added twist Robert discovers upon meeting Wong Dai, that the evil man himself has been manipulating our protagonist in order to have him kill Wong Dai&#39;s own Tong members and thus, secure the ill gotten gains of Wong Dai&#39;s empire of crime for himself.",
        "Aruba, USA",
        "English",
        "tt0323531"
    ],
    "mgname": [
        "Martial Arts Movies",
        "Crime Action & Adventure",
        "International Movies",
        "International Action & Adventure",
        "Action & Adventure",
        "Action Thrillers",
        "US Movies"
    ],
    "Genreid": [
        "8985",
        "9584",
        "78367",
        "852490",
        "1365",
        "43048",
        "1159493"
    ],
    "people": [
        {
            "actor": [
                "Corey Johnson",
                "Elaine Tan",
                "Dave Wong",
                "MC Harvey",
                "Tom Wu",
                "Michelle Goh",
                "Steven Seagal",
                "Ozzie Yue",
                "Bruce Wang",
                "Chike Chan",
                "Hon Ping Tang",
                "Kata Dobo"
            ]
        },
        {
            "creator": [
                "Danny Lerner",
                "Dennis Dimster"
            ]
        },
        {
            "director": [
                "Michael Oblowitz"
            ]
        }
    ],
    "country": [
        [
            "Japan ",
            "jp",
            "",
            "2016-02-05",
            "",
            [
                ""
            ],
            [
                "English",
                "Japanese"
            ],
            [
                "Japanese"
            ],
            "267"
        ]
    ]
  }
 }

Error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 3258 path $ .RESULT.country [0] [5] null

The error happens in the "country" part of my pojo. They were automatically made by a plugins. With several different, and all are created equal. This json is was poorly made. Can not change this

They can point me a solution.

    
asked by anonymous 06.01.2017 / 07:10

1 answer

2

The error happens because you declared the "country" field as:

@SerializedName("country")
@Expose
private List<List<String>> country;

That is, a list of string lists. The problem is that in JSON exemplified not all elements of "country" correspond to this type.

For example, the "5th" element item "country" equals:

[
    [
        [
            ""
        ],
    ]
]

What would it take to declare the "country" field as:

@SerializedName("country")
@Expose
private List<List<List<String>>> country;

Since it is not possible to change the structure of the received data the way is to treat these 'mixed' elements manually. Based on the answers to this question, the solution involves:

  • Treat only this case (example):

    String case1 = "{'field':false}";
    String case2 = "{'field':[1,2,3,4]}";
    JsonElement jsonElement = ((JsonObject)(new JsonParser().parse(case1))).get("field");
    
    if(jsonElement instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray)jsonElement;
        if(jsonArray != null && jsonArray.size() > 0) {
            for (JsonElement aJsonElement : jsonArray) {
                // TODO: handle json element inside array 
                System.out.println(aJsonElement);
            }
        }
    } else if (jsonElement instanceof JsonPrimitive) {
        boolean value = jsonElement.getAsBoolean();
        System.out.println("value:" + value);
    }
    
  • Or write a deserializer for the class in question.

16.01.2017 / 19:31