从JSON读取文件到POJO时获取空指针

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

我有下面给出的json,它以字符串形式出现。我只想将SEGMENT对象ex:(TYPE和UN_NUM)中的两个字段映射到pojo。我使用以下代码返回空值。

test.json

{
    "TEST": {
        "NAME": "PART_TRAN",
        "VERSION": "9.0",
        "ID": "----",
        "SEGMENT": {
            "TYPE": "R",
            "CLIENT_ID": "----",
            "UN_NUM": "UN"
        }
    }
}

test.java

                  process(new Processor() {
                                @Override
                                public void process(Exchange exchange) throws Exception {
                                    String data = exchange.getIn().getBody(String.class);
                                    try{
                                        XmlMapper xmlMapper = new XmlMapper();

                                        JsonNode jsonNode = xmlMapper.readTree(data.toString());

                                        ObjectMapper objectMapper = new ObjectMapper();

                                        String value = objectMapper.writeValueAsString(jsonNode);

                                        logger.info("Converting XML to JSON {}" , value);

                                        SEGMENT seg = objectMapper.readValue(value, SEGMENT.class);

                                        Test test = new Test(seg);

                                        logger.info("Test Object {}" , test);

                                    }catch (JsonParseException e){
                                        e.printStackTrace();
                                    }catch (JsonMappingException e){
                                        e.printStackTrace();
                                    }catch (IOException e){
                                        e.printStackTrace();
                                    }

                                }
                            }).     

SEGMENT.java

@Data
@JsonIgnoreProperties
public class SEGMENT {


    @JsonIgnore
    private String TYPE;

    @JsonIgnore
    private String CLIENT_ID;

    @JsonIgnore
    private String UN_NUM;

}

Test.java

@Data
public class Test {


    private String NAME;

    private String VERSION;

    private String ID;

    private SEGMENT segment;

}

日志:

: Test Object Test(SEGMENT=SEGMENT(TYPE=null, CLIENT_ID =null,UN_NUM =null))

我刚刚添加了SEGMENT类,该类用于映射json。

java json xml objectmapper
1个回答
1
投票

underscore-java库具有方法U.fromJsonMap(json)U.get(map, path)。我是该项目的维护者。

    String jsonData = "{\n"
    + "    \"TEST\": {\n"
    + "        \"NAME\": \"PART_TRAN\",\n"
    + "        \"VERSION\": \"9.0\",\n"
    + "        \"ID\": \"----\",\n"
    + "        \"SEGMENT\": {\n"
    + "            \"TYPE\": \"R\",\n"
    + "            \"CLIENT_ID\": \"----\",\n"
    + "            \"UN_NUM\": \"UN\"\n"
    + "        }"
    + "    }"
    + "}";
    Map<String, Object> jsonObject = U.fromJsonMap(jsonData);
    String type = U.<String>get(jsonObject, "TEST.SEGMENT.TYPE");
    String unNum = U.<String>get(jsonObject, "TEST.SEGMENT.UN_NUM");
    System.out.println(type);
    System.out.println(unNum);

输出:

R
UN
© www.soinside.com 2019 - 2024. All rights reserved.