带有类的嵌套对象在导入类时返回null

问题描述 投票:0回答:1

我对Java感到生疏,所以我想知道是否有人可以提供帮助。我有一个来自rest API的Json,我似乎无法读取嵌套的对象,但是字段在那里,所以我很茫然。

这是我的json字符串的样子(来自String结果的结果= response.getEntity(String.class);在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"
               }
         }

使用下面的代码,我能够毫无问题地获取firstName和lastName,但是除非获得显式的嵌套对象,否则我将无法获取它们(使用@ Serialized,@ Exposure) 。我不确定我到底在哪里出错,因为导入时没有错误:/

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
java json gson
1个回答
0
投票

您的模型类应与下面类似

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

两个课就足够了。

热门问题
推荐问题
最新问题