【问题标题】:Unable to output anything using Java and Jackson无法使用 Java 和 Jackson 输出任何内容
【发布时间】:2017-08-16 17:51:50
【问题描述】:

我在运行代码时得到 [null, null] 作为输出。有什么想法吗?

这是我的反序列化类:

package TestPackage;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DeSerializeJSON {

    private String url;
    private String ip;

    public DeSerializeJSON(){}
    //I have deserialized the rest.

    @Override
    public String toString(){
        return url + "," + ip;
    }
}

我的测试类如下:

ObjectMapper mapper = new ObjectMapper();

        try{

            List<DeSerializeJSON> urls = Arrays.asList(mapper.readValue(new File("C:\\Test\\Output.txt").getAbsoluteFile(), DeSerializeJSON[].class));
            PrintStream out = new PrintStream(new FileOutputStream("C:\\Test\\ipUrlOutput.txt"));
            System.setOut(out);
            System.out.println(urls);

【问题讨论】:

  • 显示你的 JSON。
  • 这是我的 JSON:[ { "items": [ { "Inputs": { "url": "some.com" }, "Outputs": { "ip": "51.70.182.125 ", "redirectipaddress": "16.150.210.199", "httpstatuscode": "200", "processes": {}, "Steps": [], "Queues": {}, "Outcomes": { "language":空,“类型”:空 } } } ] } ]
  • 首先,给定的 JSON 无效。在some.com 之后有一个意想不到的;。删除它以使其有效。

标签: java json jackson deserialization


【解决方案1】:

怎么了

您的 Java 类与您的 JSON 不匹配,这就是为什么您有 null 值并且您没有收到任何映射异常,因为您的类使用 @JsonIgnoreProperties(ignoreUnknown = true) 注释。

如何解决

考虑一下您在comment 上发布的 JSON 文档:

[
  {
    "items": [
      {
        "Inputs": {
          "url": "some.com"
        },
        "Outputs": {
          "ip": "51.70.182.125",
          "redirectipaddress": "16.150.210.199",
          "httpstatuscode": "200",
          "processes": {},
          "Steps": [],
          "Queues": {},
          "Outcomes": {
            "language": null,
            "type": null
          }
        }
      }
    ]
  }
]

需要解析以下类:

public class ItemsWrapper {

    @JsonProperty("items")
    private List<Item> items;

    // Default constructor, getters and setters
}
public class Item {

    @JsonProperty("Inputs")
    private Input input;

    @JsonProperty("Outputs")
    private Output output;

    // Default constructor, getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Input {

    @JsonProperty("url")
    private String url;

    // Default constructor, getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Output {

    @JsonProperty("ip")
    private String ip;

    // Default constructor, getters and setters
}

然后你可以使用ObjectMapper解析JSON:

String json = "[\n" +
              "  {\n" +
              "    \"items\": [\n" +
              "      {\n" +
              "        \"Inputs\": {\n" +
              "          \"url\": \"some.com\"\n" +
              "        },\n" +
              "        \"Outputs\": {\n" +
              "          \"ip\": \"51.70.182.125\",\n" +
              "          \"redirectipaddress\": \"16.150.210.199\",\n" +
              "          \"httpstatuscode\": \"200\",\n" +
              "          \"processes\": {},\n" +
              "          \"Steps\": [],\n" +
              "          \"Queues\": {},\n" +
              "          \"Outcomes\": {\n" +
              "            \"language\": null,\n" +
              "            \"type\": null\n" +
              "          }\n" +
              "        }\n" +
              "      }\n" +
              "    ]\n" +
              "  }\n" +
              "]";

ObjectMapper mapper = new ObjectMapper();
ItemsWrapper[] itemsWrappers = mapper.readValue(json, ItemsWrapper[].class);

【讨论】:

    【解决方案2】:

    试试这个com.fasterxml.jackson.core.type.TypeReference;

    List<DeSerializeJSON> urls = mapper.readValue(new File("C:\\Test\\Output.txt").getAbsoluteFile(), new TypeRefference<List<DeSerializeJSON>>(){});
    

    【讨论】:

    • 这个我试过了,好像是readValue方法不支持TypeRefference。
    • 不支持是什么意思?
    • 程序显示以下错误:ObjectMapper 类型中的方法 readValue(File, Class) 不适用于参数 (File, new TypeReference>(){ })
    • 你导入com.fasterxml.jackson.core.type.TypeReference了吗?
    • 首先,当 OP 使用 Jackson 1.xorg.codehaus.jackson 包)。其次,TypeReference 在这里无济于事:Java 类与 OP 试图解析的 JSON 不匹配。有关如何设计 Java 类的详细信息,请参阅我的 answer
    猜你喜欢
    • 2010-12-15
    • 2012-12-31
    • 1970-01-01
    • 2020-05-04
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多