Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 当值包含列表且对列表的引用为空时,如何在JSON中迭代映射_Java_Spring_Rest_Jackson - Fatal编程技术网

Java 当值包含列表且对列表的引用为空时,如何在JSON中迭代映射

Java 当值包含列表且对列表的引用为空时,如何在JSON中迭代映射,java,spring,rest,jackson,Java,Spring,Rest,Jackson,此类返回JSON字符串,但包含列表的映射值返回空指针异常 import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.

此类返回JSON字符串,但包含列表的映射值返回空指针异常

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    Map<String, Foo> foos = new HashMap<String, Foo>();
    foos.put("foo1", new Foo("foo1"));
    foos.put("foo2", new Foo(List<DetailMessage>));
    foos.put("foo3", null);
    foos.put(null, new Foo("foo4"));

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.setSerializationInclusion(Include.NON_NULL);
    // Since the DetailMessage field is null , Its throwing Null pointer //exception.
  }
}
尝试了上面提到的各种选项,仍然得到相同的空指针异常

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    Map<String, Foo> foos = new HashMap<String, Foo>();
    foos.put("foo1", new Foo("foo1"));
    foos.put("foo2", new Foo(List<DetailMessage>));
    foos.put("foo3", null);
    foos.put(null, new Foo("foo4"));

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.setSerializationInclusion(Include.NON_NULL);
    // Since the DetailMessage field is null , Its throwing Null pointer //exception.
  }
}
import java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入com.fasterxml.jackson.annotation.JsonInclude.Include;
导入com.fasterxml.jackson.core.JsonGenerator;
导入com.fasterxml.jackson.core.JsonProcessingException;

导入com.fasterxml.jackson.databind.JsonSerializer;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.SerializationFeature;
导入com.fasterxml.jackson.databind.SerializerProvider; 公务舱杰克逊福 { 公共静态void main(字符串[]args)引发异常 { Map foos=新的HashMap(); foos.put(foo1),新Foo(foo1); foos.put(“foo2”,新的Foo(列表)); foos.put(“foo3”,空); foos.put(空,新Foo(“foo4”)); ObjectMapper mapper=新的ObjectMapper(); configure(SerializationFeature.WRITE\u NULL\u MAP\u值,false); mapper.setSerializationInclusion(Include.NON_NULL); //由于DetailMessage字段为null,因此其引发null指针//异常。 } }
您遇到的问题是由于映射中的键为
null
值,默认的JSON结构如下所示

{
   "Key1" : "Value1",
   "Key2" : "Value2"...
} (This is the simplest form)
在JSON中,我们不能有任何类似于
null:“value”
的内容,这违反了基本的JSON原则,因此对象映射器无法将映射转换为JSON字符串,您需要在映射中拥有所有非null键,这样才能工作

因此,删除行
foos.put(null,newfoo(“foo4”)将使您的代码正常工作

即使Map允许1个NULL键,JSON也不符合它,所以您将面临这个错误

希望有帮助


祝你好运

谢谢,我在foos.put(“foo2”,新的Foo(List))行中遇到了异常;未设置空指针作为字段。我猜这是同一个错误,您需要显示
DetailMessage
中存在的代码,以及如何实际插入映射
foos.put(“foo2”,新Foo(List))
在正常情况下应该是编译错误com.fasterxml.jackson.databind.JasonMappingException:(was java.lang.NullPointerException)(通过引用链:java.util.HashMap[“DetailMesage”]->java..util.ArrayList[0]->com.test.container.DetailMessage[fileMapper1])