简体   繁体   中英

Nested object with Gson returns null when class is imported

I'm rusty on my Java so I was wondering if anyone could help. I have a Json that comes in from a rest API and I can't seem to get the nested objects to get read, but the fields are there so I'm at a loss.

Here is what my json string looks like (result from String result = response.getEntity(String.class); in wrapper.java)

result = "{"FIRST_NAME":"Test First","LAST_NAME":"Test Last","testAttr":[{"MOTHER_NAME":"Test Mother 1","FATHER_NAME":"Test Father 1"}, {"MOTHER_NAME":"Test Mother 2","FATHER_NAME":"Test Father 2"}]}"

(to better read it)
result = {
   "FIRST_NAME": "Test First",
   "LAST_NAME": "Test Last",
   "testAttr": {
                 "MOTHER_NAME":"Test Mother 1",
                 "FATHER_NAME":"Test Father 1"
               },
               {
                 "MOTHER_NAME":"Test Mother 2",
                 "FATHER_NAME":"Test Father 2"
               }
         }

Using the code below, I was able to get the firstName and lastName without a problem, but I wasn't able to get the nested objects unless I explicitly had them put inside the same with (with the @Serialized, @Exposure). I'm not sure where exactly I've gone wrong since I have no errors with importing:/

Main.java

@GET
@Path("/api/test")
@Produces(MediaType.APPLICATION_JSON)
public Response getAttributrs(@Context HttpServletRequest req) {
        ResponseObj responseObj = new ResponseObj();
        try {
            ResponseObj listOfAttr = wrapper.getAttr();
            return Response.ok(listOfAttr).build();
        } catch (Exception e) {
            wrapper.manageError(e, responseObj) ;
            return Response.status(500).entity(responseObj).build();
        } 
    }
Wrapper.java

public ResponseObj getAttr() throws Exception {

        Client client = ClientHelper.createClient();
        WebResource webResource = client
                .resource("https://xxxxxxxxx);
        webResource.header("X-METHOD-OVERRIDE", "GET");
        webResource.header("content-type", "application/json");
        ClientResponse response = webResource.type("application/json").get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
        String result = response.getEntity(String.class);
        ResponseObj responseObj = new Gson().fromJson(result, ResponseObj.class);
        return responseObj;
    }
ResponseObj.java

@SerializedName("testAttr")
@Expose
private List<AttributesClass> testAttributes;

// getters/setters
AttributeClass.java

private TestInnerClass testInnerClass;
@SerializedName("FIRST_NAME")
@Expose
private String firstName;
@SerializedName("LAST_NAME")
@Expose
private String lastName;

//getters/setters
TestInnerClass.java
@SerializedName("MOTHER_NAME")
@Expose
private String mothersName;
@SerializedName("FATHER_NAME")
@Expose
private String fathersName;

//getters/setters

Your model classes should be like below

Class 1

public class ResponseObj {
@SerializedName("FIRST_NAME")
private String fIRSTNAME;
@SerializedName("LAST_NAME")
private String lASTNAME;
@SerializedName("testAttr")
private List<AttributeClass> testAttr = null;
// getter and setter

Class2

public class AttributeClass {
@SerializedName("MOTHER_NAME")
private String mOTHERNAME;
@SerializedName("FATHER_NAME")
private String fATHERNAME;
// getter and setter

two classes are sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM