Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Java和Jackson输出任何内容_Java_Json_Jackson_Deserialization - Fatal编程技术网

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

无法使用Java和Jackson输出任何内容,java,json,jackson,deserialization,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;

我在运行代码时得到[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);
ObjectMapper mapper=new ObjectMapper();
试一试{
列表URL=Arrays.asList(mapper.readValue(新文件(“C:\\Test\\Output.txt”).getAbsoluteFile(),反序列化JSON[].class));
PrintStream out=新的PrintStream(新的FileOutputStream(“C:\\Test\\ipUrlOutput.txt”);
系统放样;
System.out.println(URL);

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

List<DeSerializeJSON> urls = mapper.readValue(new File("C:\\Test\\Output.txt").getAbsoluteFile(), new TypeRefference<List<DeSerializeJSON>>(){});
List url=mapper.readValue(新文件(“C:\\Test\\Output.txt”).getAbsoluteFile(),新类型引用(){});
怎么了 您的Java类与您的JSON不匹配,这就是为什么您有
null
值,并且您不会得到任何映射异常,因为您的类使用
@JsonIgnoreProperties(ignoreUnknown=true)
注释

如何修复它 以您在网站上发布的JSON文档为例:

[
{
“项目”:[
{
“投入”:{
“url”:“some.com”
},
“产出”:{
“ip”:“51.70.182.125”,
“重定向IP地址”:“16.150.210.199”,
“httpstatuscode”:“200”,
“进程”:{},
“步骤”:[],
“队列”:{},
“成果”:{
“语言”:空,
“类型”:空
}
}
}
]
}
]
它需要解析以下类:

公共类项目说唱者{
@JsonProperty(“项目”)
私人清单项目;
//默认构造函数、getter和setter
}
公共类项目{
@JsonProperty(“输入”)
私人投入;
@JsonProperty(“输出”)
私人产出;
//默认构造函数、getter和setter
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类输入{
@JsonProperty(“url”)
私有字符串url;
//默认构造函数、getter和setter
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类输出{
@JsonProperty(“ip”)
私有字符串ip;
//默认构造函数、getter和setter
}
然后可以使用
ObjectMapper
解析JSON:

String json=“[\n”+
“{\n”+
“\'items\”:[\n”+
“{\n”+
“\”输入\“:{\n”+
“\“url\”:\“some.com\”\n”+
},\n+
“\”输出\“:{\n”+
“ip\:\“51.70.182.125\,\n”+
“重定向IP地址”:“16.150.210.199\,\n”+
“\“httpstatuscode\”:\“200\”,\n”+
“\”进程\:{},\n”+
“\”步骤\:[],\n”+
“\”队列\:{},\n”+
“\”结果\“:{\n”+
“语言”:空,\n+
“\“类型\”:空\n”+
“}\n”+
“}\n”+
“}\n”+
“]\n”+
“}\n”+
"]";
ObjectMapper mapper=新的ObjectMapper();
ItemsWrapper[]itemsWrappers=mapper.readValue(json,ItemsWrapper[].class);

显示您的JSON。这是我的JSON:[{“items”:[{“Inputs”:{“url”:“}”,Outputs:{“ip”:“51.70.182.125”,“redirectipaddress”:“16.150.210.199”,“httpstatuscode”:“200”,“processs”:{},“步骤”:[],“队列”:{},“结果”:{“语言”:null,“类型”:null}}]首先,给定的JSON无效。就在
some.com
之后出现了一个意外的
。请将其删除以使其有效。我尝试了这个方法,似乎是readValue方法不支持TypeRefReference。你说不支持是什么意思?程序显示以下错误:方法readValue(文件,类)在类型中,ObjectMapper不适用于参数(文件,new TypeReference(){}),您是否导入了
com.fasterxml.jackson.core.type.TypeReference
?首先,您建议在OP使用jackson 1.x(
org.codehaus.jackson
包)时使用jackson 2.x(
com.fasterxml.jackson
包)解决方案。其次,
TypeReference
在这里没有帮助:Java类与OP试图解析的JSON不匹配。有关Java类应如何设计的详细信息,请参阅my。