如何解析对单个JAVA对象的JSON响应

时间:2018-10-18 08:54:10

标签: java json jackson

我试图解析对单个JSON对象的JAVA响应。此JSON响应包含在另一个JSON数组和简单字段中。

但是我在尝试中可能会得到以下结果。

13:07:25,769 INFO  [biz.example.ClentManager] (default task-106) Start findBasicAccountInformation intigartion................................
13:07:25,769 INFO  [biz.example.ClentManager] (default task-106) Account No................................200300008212
13:07:25,831 INFO  [biz.example.ClentManager] (default task-106) Response : Response{protocol=http/1.1, code=200, message=OK, url=http://192.168.13.40:8092/v2/accounts/200300008212}
13:07:25,832 INFO  [biz.example.ClentManager] (default task-106) satatus======================>>>OK
13:07:25,832 INFO  [biz.example.ClentManager] (default task-106) statusCode======================>>>200
13:07:25,832 INFO  [biz.example.ClentManager] (default task-106) Start : outputJSONObjects
13:07:25,832 INFO  [biz.example.ClentManager] (default task-106) jsout : {"StatusCode":200,"TraceId":"89C8F03DC3D946A89F43AE94E0B151A0","Status":"SUCCESS","ErrorList":[],"Result":{"CustomFields":[],"AccountNumber":"200300008212","BranchId":"003","Name":"SAHAN MADURANGA","ShortName":"GHJJKGDJ","CustomerId":"358106","SchemeCode":"SA000","SchemeType":"SBA","Currency":"LKR","Ownership":"E","ChargeLevelCode":"GENRL","CloseFlag":"N","DormantStatus":"A","FrezeCode":" ","JointStatus":"M","ModeOfOpertionalCode":null,"CustomerRelationCode":" ","MinorStatus":"N","TransactionStatus":"T","OpenDate":"2011-12-17T00:00:00"}}
13:07:33,793 ERROR [biz.example.ClentManager] (default task-106) com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "StatusCode" (class biz.spsolutions.edgevantage.workflow.dao.PABCIntegration.ResultBasicAccountInformationDao), not marked as ignorable (5 known properties: "traceId", "status", "result", "errorList", "statusCode"])
 at [Source: N/A; line: -1, column: -1] (through reference chain: biz.spsolutions.edgevantage.workflow.dao.PABCIntegration.ResultBasicAccountInformationDao["StatusCode"])
13:10:01,933 WARNING [com.hazelcast.spi.impl.operationservice.impl.InvocationMonitor] (hz._hzInstance_1_dev.InvocationMonitorThread) [IP]:5701 [dev] [3.8.1] MonitorInvocationsTask delayed 147145 ms
13:10:01,933 WARNING [com.hazelcast.spi.impl.operationservice.impl.InvocationMonitor] (hz._hzInstance_1_dev.InvocationMonitorThread) [IP]:5701 [dev] [3.8.1] BroadcastOperationControlTask delayed 145689 ms
13:10:02,021 INFO  [biz.example.ClentManager] (default task-106) End findBasicAccountInformation intigartion................................

你有什么主意吗?

2 个答案:

答案 0 :(得分:1)

我认为这是对错误的总结: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ StatusCode ”(类biz.spsolutions.edgevantage.workflow.dao.PABCIntegration.ResultBasicAccountInformationDao),未标记为可忽略(5个已知属性: “ traceId”,“ status”,“结果”,“ errorList”,“ statusCode ”]]

似乎字段名称不匹配,一个以大写S开头,另一个以小写S开头。请尝试更改它。

答案 1 :(得分:1)

您可以使用类似这样的东西:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class MyDTO implements Serializable {

    @JsonProperty("id")
    private Long id;


    @JsonProperty("tax_id")
    private Long taxId;

    @JsonProperty("thumbnail")
    private String thumbnail;

    @JsonProperty("short_description")
    private Map<String,Object> shortDescription;

    public static MyDTO buildCatalogProductDTO(MyDomain md) {
    return MyDTO.builder()
            .id(md.getId())
            .taxId(catalogProduct.getTaxId())
            .shortDescription(SerializationUtil.parseJson(md.getShortDescription()))
            .build();
    }

其中SerializationUtil.parseJson在内部使用Gson库进行转换。