【问题标题】:unable to serialize JSON to Set correctly with Spring MVC/jackson无法使用 Spring MVC/jackson 序列化 JSON 以正确设置
【发布时间】:2014-08-25 08:57:13
【问题描述】:
public class Parent {

    private List<Child> childs;

}

public class Parent {

    private Set<Child> childs;

}

@RequestMapping(value = "/save", method = RequestMethod.POST)
public void saveFooter(@RequestBody Parent parent, Model model)

以下是示例 JSON 对象。

{child: [{ name: 'a'}, { name: 'b'}]}

当我在 Parent.class,parent.getChilds.size() 中为子元素使用 Set 时,子元素的大小仅为 1(这是错误的)。

但是当我在Parent.class parent.getChilds.size() 中对childs 使用List 时,childs 的大小是2(这是正确的)

编辑:

public class Child {

    private String name;

}

jackson maven 依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hibernate4</artifactId>
            <version>2.4</version>
        </dependency>

编辑:

public class Child {
    private Long id;
    private String name;

    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Child other = (Child) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    } 
}

发现问题是由于我实现了 Child.class 的 equals 方法 这使得两个对象相等。

【问题讨论】:

  • 添加jackson-core和jackson-mapper依赖
  • 您能出示您的Child 文件吗?你为这个类实现了equals方法吗?
  • 您的子实例是什么样的?集合是不允许重复的集合。如果您将一个元素添加到与另一个相同的集合中,则只会保留一个实例。
  • 我检查了你的课程Set,它对我有用。你能展示你真正的Child 类(带有hashCode,equals)方法吗?
  • 感谢@MichałZiober 和 matsev,我能够找出原因

标签: java json spring-mvc jackson


【解决方案1】:

Parent.java

package com.test;

import java.util.Set;

public class Parent {

    private Set<Child> childs;

    public Set<Child> getChilds() {
        return childs;
    }

    public void setChilds(Set<Child> childs) {
        this.childs = childs;
    }
}

Child.Java

package com.test;

public class Child {
    private String name;

    public Child(String name) {
        this.name = name;
    }

    public Child() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Test.java

    package com.test;

import java.io.IOException;
import java.util.HashSet;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import flexjson.JSONSerializer;

public class MvelCompilation {

    public static void main(String[] args) {

        Parent parent = new Parent();
        Child c1 = new Child("Ankur1");
        Child c2 = new Child("Ankur2");

        HashSet<Child> childSet = new HashSet<Child>();
        childSet.add(c1);
        childSet.add(c2);
        parent.setChilds(childSet);

        JSONSerializer serializer = new JSONSerializer();
        System.out.println("Json String is = " + serializer.exclude("*.class").deepSerialize(parent));

        String s = "{\"childs\":[{\"name\":\"Ankur1\"},{\"name\":\"Ankur2\"}]}";

        try {
            Parent parent1 = new ObjectMapper().readValue(s, Parent.class);
            System.out.println("size = " + parent1.getChilds().size());
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

输出

Json 字符串为 = {"childs":[{"name":"Ankur1"},{"name":"Ankur2"}]}

大小 = 2

【讨论】:

    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2012-04-06
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多