无法使用Java和Jackson输出任何内容

时间:2017-08-16 17:51:50

标签: java json jackson deserialization

运行代码时,我得到[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);

2 个答案:

答案 0 :(得分:0)

出了什么问题

您的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);

答案 1 :(得分:-1)

尝试使用此com.fasterxml.jackson.core.type.TypeReference;

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