Jackson ObjectMapper不读取内部对象

发布于 2025-01-17 15:05:41 字数 3571 浏览 0 评论 0原文

我有一个json文件

{
    "readServiceAuthorizationResponse": {
        "serviceAuthorization": {
            "serviceAuthorizationId": "50043~220106065198",
            "status": "Approved",
            "receivedDate": "2022-1-6 1:21:12 PM",
            "providerFirstName": "Ranga",
            "providerLastName": "Thalluri",
            "organizationName": "General Hospital",
            "serviceLines": [{
                "statusReason": "Approved",
                "procedureDescription": "Room & board ward general classification",
                "requestedQuantity": "1.00",
                "approvedQuantity": "1.00",
                "deniedQuantity": "",
                "quantityUnitOfMeasure": "Day(s)",
                "providers": [{
                    "providerFirstName": "Ranga",
                    "providerLastName": "Thalluri",
                    "organizationName": ""
                }]
            }]
        }
    }
}

我的java将其读取为一个对象是:

package com.shawn.dto;

import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceAuthorizationDTO {
    public String serviceAuthorizationId;
    public String status;
    public String receivedDate;
    public String providerFirstName;
    public String providerLastName;
    public String organizationName;
    public ServiceLine[] serviceLines;
    
    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper m = new ObjectMapper();
        try {
            Outer outer = m.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class ReadServiceAuthorizationResponse {
        public ServiceAuthorizationDTO serviceAuthorization;        

    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Outer {
        public ReadServiceAuthorizationResponse readServiceAuthorizationResponse;       
        
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ServiceLine {
        String statusReason;
        String procedureDescription;
        String requestedQuantity;
        String approvedQuantity;
        String deniedQuantity;
        String quantityUnitOfMeasure;
        Provider[] providers;
        
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Provider {
        String providerFirstName;
        String providerLastName;
        String organizationName;

    }
    
    public static void main(String[] args) {
        try {
            String json = new String(Files.readAllBytes(Paths.get("c:/temp/test.json")));
            ServiceAuthorizationDTO dao = ServiceAuthorizationDTO.create(json);
            System.out.println("serviceAuthorizationId: " + dao.serviceAuthorizationId);
            System.out.println("serviceLines[0].procedureDescription: " + dao.serviceLines[0].procedureDescription);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
}

当我运行时,我会得到:

serviceAuthorizationId: 50043~220106065198
serviceLines[0].procedureDescription: null

从JSON读取对象中的外部字段。但是Servicelines数组显示1个元素,该类中的所有字段都是空的。

有什么想法吗?这是我第一次与JSON一起使用真实对象。我总是将其映射到地图对象中,并手动将字段拉出。谢谢。

I have a JSON file

{
    "readServiceAuthorizationResponse": {
        "serviceAuthorization": {
            "serviceAuthorizationId": "50043~220106065198",
            "status": "Approved",
            "receivedDate": "2022-1-6 1:21:12 PM",
            "providerFirstName": "Ranga",
            "providerLastName": "Thalluri",
            "organizationName": "General Hospital",
            "serviceLines": [{
                "statusReason": "Approved",
                "procedureDescription": "Room & board ward general classification",
                "requestedQuantity": "1.00",
                "approvedQuantity": "1.00",
                "deniedQuantity": "",
                "quantityUnitOfMeasure": "Day(s)",
                "providers": [{
                    "providerFirstName": "Ranga",
                    "providerLastName": "Thalluri",
                    "organizationName": ""
                }]
            }]
        }
    }
}

My Java to read this into an object is this:

package com.shawn.dto;

import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceAuthorizationDTO {
    public String serviceAuthorizationId;
    public String status;
    public String receivedDate;
    public String providerFirstName;
    public String providerLastName;
    public String organizationName;
    public ServiceLine[] serviceLines;
    
    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper m = new ObjectMapper();
        try {
            Outer outer = m.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class ReadServiceAuthorizationResponse {
        public ServiceAuthorizationDTO serviceAuthorization;        

    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Outer {
        public ReadServiceAuthorizationResponse readServiceAuthorizationResponse;       
        
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ServiceLine {
        String statusReason;
        String procedureDescription;
        String requestedQuantity;
        String approvedQuantity;
        String deniedQuantity;
        String quantityUnitOfMeasure;
        Provider[] providers;
        
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Provider {
        String providerFirstName;
        String providerLastName;
        String organizationName;

    }
    
    public static void main(String[] args) {
        try {
            String json = new String(Files.readAllBytes(Paths.get("c:/temp/test.json")));
            ServiceAuthorizationDTO dao = ServiceAuthorizationDTO.create(json);
            System.out.println("serviceAuthorizationId: " + dao.serviceAuthorizationId);
            System.out.println("serviceLines[0].procedureDescription: " + dao.serviceLines[0].procedureDescription);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
}

When I run it I get this:

serviceAuthorizationId: 50043~220106065198
serviceLines[0].procedureDescription: null

The outer fields in the object like providerId are read from the JSON. But the serviceLines array shows 1 element, and all fields in that class are empty.

Any ideas? This is the first time I've used real objects with JSON. I've always mapped it into Map objects and pulled the fields out manually. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-24 15:05:41

类中的字段Serviceline提供商具有package-Provate访问修饰符。杰克逊(Jackson)无法通过其默认设置进行私人字段。因为它需要Getter或Setter方法。

解决方案1:公开场

    public static class ServiceLine {
        public String statusReason;
        public String procedureDescription;
        public String requestedQuantity;
        public String approvedQuantity;
        public String deniedQuantity;
        public String quantityUnitOfMeasure;
        public Provider[] providers;        
    }

解决方案2:使用@jsonautodetect 注释

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class ServiceLine {
    String statusReason;
    String procedureDescription;
    String requestedQuantity;
    String approvedQuantity;
    String deniedQuantity;
    String quantityUnitOfMeasure;
    Provider[] providers;
}

解决方案3:更改ObjectMapper上的可见性( doc

    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            Outer outer = objectMapper.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }

Fields in classes ServiceLine and Provider have package-private access modifiers. Jackson can't deserialize into private fields with its default settings. Because it needs getter or setter methods.

Solution 1: Make fields public

    public static class ServiceLine {
        public String statusReason;
        public String procedureDescription;
        public String requestedQuantity;
        public String approvedQuantity;
        public String deniedQuantity;
        public String quantityUnitOfMeasure;
        public Provider[] providers;        
    }

Solution 2: Use @JsonAutoDetect annotation

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class ServiceLine {
    String statusReason;
    String procedureDescription;
    String requestedQuantity;
    String approvedQuantity;
    String deniedQuantity;
    String quantityUnitOfMeasure;
    Provider[] providers;
}

Solution 3: Change visibility on the ObjectMapper (doc)

    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            Outer outer = objectMapper.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文