杰克逊JSON解析器保持回归初始值

时间:2018-02-14 17:42:28

标签: java json jackson

我的程序使用SQL构建一个URL来调用web服务,这个服务将返回以下JSON:

null

我要做的是剥离“True”或“False”并使用该值来执行其他逻辑。这是我构建URL后的代码: while(rs1.next()){

[{
    "MmisItemNo": "106552",
    "CatalogNo": "UM18840041R",
    "ContractOn": "False"
 }
]

这是我的LightswitchResponse类:

String first = (rs1.getString("PROP_VALUE"));   
String second = (rs1.getString("VEN_ITEM"));

String url = (first.trim()+second.trim());

URL obj = new URL(url);

HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

int  responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " +url);
System.out.println("Response Code : " + responseCode);


BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
    }
    in.close();
    System.out.println(response);

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
LightswitchResponse[] lightswitchResponses = mapper.readValue((response.toString()), LightswitchResponse[].class);
LightswitchResponse lightswitchResponse = lightswitchResponses[0];
System.out.println(lightswitchResponse.ContractOn);
}
} catch (SQLException q) {
logger.error(q);
System.exit(1);
} catch (Exception e) {
logger.error(e,e);
System.exit(1); 
} finally {
}
logger.debug("getlightswitchQuery(): End");

我的问题是,当我打印我的结果=> ContractOn只为每一行记录/ ​​json返回false,无论其是True还是False。

例如:

public class LightswitchResponse {

String MmisItemNo;
String CatalogNo;
boolean ContractOn;

public String getMmisItemNo() {
    return MmisItemNo;
}
public void setMmisItemNo(String mmisItemNo) {
    MmisItemNo = mmisItemNo;
}
public String getCatalogNo() {
    return CatalogNo;
}
public void setCatalogNo(String catalogNo) {
    CatalogNo = catalogNo;
}
public boolean ContractOn() {
    return ContractOn;
}
public void setContractOn(boolean contractOn) {
    ContractOn = contractOn;
}

}

我原本以为在我的缓冲区中有些东西没有清除,但在深入挖掘之后,我认为情况并非如此。不管字符串响应如何,可能会导致这个重复错误的问题是什么?

1 个答案:

答案 0 :(得分:0)

这里的主要问题是您使用的是JSON的非标准Java命名约定。

按如下方式注释您的LightswitchResponse将起作用:

public static class LightswitchResponse {

    String MmisItemNo;
    String CatalogNo;
    boolean ContractOn;

    public String getMmisItemNo() {
        return MmisItemNo;
    }

    @JsonProperty("MmisItemNo")
    public void setMmisItemNo(String mmisItemNo) {
        MmisItemNo = mmisItemNo;
    }
    public String getCatalogNo() {
        return CatalogNo;
    }

    @JsonProperty("CatalogNo")
    public void setCatalogNo(String catalogNo) {
        CatalogNo = catalogNo;
    }

    public boolean ContractOn() {
        return ContractOn;
    }

    @JsonProperty("ContractOn")
    public void setContractOn(boolean contractOn) {
        ContractOn = contractOn;
    }

}

以下测试通过OK:

@Test
public void testDeserializeBoolean() throws IOException{
    final String test = "[{\"MmisItemNo\": \"106552\",\"CatalogNo\": \"UM18840041R\",\"ContractOn\": \"False\"}," +
            "{\"MmisItemNo\": \"106552\",\"CatalogNo\": \"UM18840041R\",\"ContractOn\": \"True\"}]";
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    LightswitchResponse[] resp = objectMapper.readValue(test
            ,
            LightswitchResponse[].class);
    Assert.assertEquals(false, resp[0].ContractOn());
    Assert.assertEquals(true, resp[1].ContractOn());
}