Jared Hart Jared Hart - 3 months ago 6
Java Question

Java ; JSON ; Jackson ; Retrieving the value from a List inside an Array from JSON

I'm trying to read the values from a JSON URL, however I don't know how I can proceed with reading the values from a List inside of an Array? Below you will find my POJO, Main, and JSON code. Thank you so much for your help

POJO:

package org.jcexchange.FBApp;
import java.util.List;
import org.jcexchange.FBApp.Details;


public class Users {
private List<Details> Values;

public List<Details> getValues() {
return this.Values;
}
public void setValues(List<Details> Values) {
this.Values = Values;
}
}

public class Details {
private String user_name;
private String user_password;
private int age;
private String user_email;

public String getUserName() {
return this.user_name;
}
public void setUserName(String user_name) {
this.user_name = user_name;
}

public String getUserPassword() {
return this.user_password;
}
public void setUserPassword(String user_password) {
this.user_password = user_password;
}

public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}

public String getUserEmail() {
return this.user_email;
}
public void setUserEmail(String user_email) {
this.user_email = user_email;
}
}


Main:

public class Main {
public static void main(String[] args) {
try {
URL jsonURL = new URL("https://jchtest.herokuapp.com/index.php?
PW=2");
ObjectMapper mapper = new ObjectMapper();




mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
Users[] a1 = mapper.readValue(jsonURL, Users[].class);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}


I'm able to pull the JSON from a webservice, however I'm stuck trying to figure out how I could retrieve for instance the user_name from the first "Values" index of the JSON array

JSON:

[
{
"Values": {
"user_name": "jhart",
"user_password": "gooddeval1",
"age": 28,
"user_email": "heheh"
}
},
{
"Values": {
"user_name": "bdole",
"user_password": "Passwordd",
"age": 82,
"user_email": "hahah"
}
}
]

Answer

Well , it is a little confusing here may be because i dont have the full context. From the de-serializer you are telling me that i expect an Array of Users and then within each User i have a List of "Values" , but the JSON tells me that Values is a singular property for Users. Anyways , here is a sample that works on the assumption i have made. This can be fiddled around to change the collection and singular properties

import org.codehaus.jackson.annotate.JsonProperty;

public class Users {

    @JsonProperty("Values")
    private Details Values;

    public Details getValues() {
        return this.Values;
    }

    public void setValues(Details Values) {
        this.Values = Values;
    }
}



import org.codehaus.jackson.annotate.JsonProperty;

public class Details {
@JsonProperty("user_name")
private String user_name;
@JsonProperty("user_password")
private String user_password;
@JsonProperty("age")
private int age;
@JsonProperty("user_email")
private String user_email;

public String getUserName() {
    return this.user_name;
}

public void setUserName(String user_name) {
    this.user_name = user_name;
}

public String getUserPassword() {
    return this.user_password;
}

public void setUserPassword(String user_password) {
    this.user_password = user_password;
}

public int getAge() {
    return this.age;
}

public void setAge(int age) {
    this.age = age;
}

public String getUserEmail() {
    return this.user_email;
}

public void setUserEmail(String user_email) {
    this.user_email = user_email;
}

}

import java.net.URL;

import org.codehaus.jackson.map.ObjectMapper;

public class Main {
    public static void main(String[] args) {
        try {
            URL jsonURL = new URL("https://jchtest.herokuapp.com/index.php?PW=2");
            ObjectMapper mapper = new ObjectMapper();
            Users[] a1 = mapper.readValue(jsonURL, Users[].class);
            System.out.println(a1[0].getValues().getUserName());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

This prints "jhart" for me.

Please note : One thing you can try is based on the array/singular property you can populate the Object and write it as JSON. That way you can find what is different in what Jackson Deserializer expects vs What we are actually supplying.