简体   繁体   English

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

[英]How to parse JSON response to a single JAVA Object

I tried to parse a JSON response to a single JAVA Object.This JSON response included with another JSON arrays and simple fields. 我试图解析对单个JAVAJSON响应,该JSON响应包含在另一个JSON数组和简单字段中。

However I could get following result in my attempts. 但是,在尝试中我可以得到以下结果。

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................................

Do you have any idea ? 你有什么主意吗 ?

I think this sums up the error:- 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 "]) 我认为这可以总结出以下错误:-com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ StatusCode ”(类biz.spsolutions.edgevantage.workflow.dao.PABCIntegration.ResultBasicAccountInformationDao),未标记为可忽略(5个已知)属性:“ traceId”,“状态”,“结果”,“ errorList”,“ statusCode ”])

seems the field names do not match, one starts with capital S and the other small s. 字段名称似乎不匹配,一个以大写S开头,另一个以小写S开头。 Please try changing it. 请尝试更改它。

You can use some thing like this: 您可以使用以下方法:

@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();
    }

where SerializationUtil.parseJson is internally using Gson library for conversion. 其中SerializationUtil.parseJson在内部使用Gson库进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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