在杰克逊的数据绑定的文档表明,杰克逊支持deserialising"所有支持类型的数组",但我想不通这个确切的语法.
对于单个对象,我会这样做:
//json input
{
    "id" : "junk",
    "stuff" : "things"
}
//Java
MyClass instance = objectMapper.readValue(json, MyClass.class);
Run Code Online (Sandbox Code Playgroud)
现在对于一个数组我想这样做:
//json input
[{
    "id" : "junk",
    "stuff" : "things"
},
{
    "id" : "spam",
    "stuff" : "eggs"
}]
//Java
List<MyClass> entries = ?
Run Code Online (Sandbox Code Playgroud)
有人知道是否有一个神奇的缺失命令?如果没有那么解决方案是什么?
如果该字段的值为null,Jackson如何配置为在序列化期间忽略字段值.
例如:
public class SomeClass {
   // what jackson annotation causes jackson to skip over this value if it is null but will 
   // serialize it otherwise 
   private String someValue; 
}
Run Code Online (Sandbox Code Playgroud) 我需要将某个JSON字符串转换为Java对象.我正在使用Jackson进行JSON处理.我无法控制输入JSON(我从Web服务中读取).这是我的输入JSON:
{"wrapper":[{"id":"13","name":"Fred"}]}
Run Code Online (Sandbox Code Playgroud)
这是一个简化的用例:
private void tryReading() {
    String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";
    ObjectMapper mapper = new ObjectMapper();  
    Wrapper wrapper = null;
    try {
        wrapper = mapper.readValue(jsonStr , Wrapper.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("wrapper = " + wrapper);
}
Run Code Online (Sandbox Code Playgroud)
我的实体类是:
public Class Student { 
    private String name;
    private String id;
    //getters & setters for name & id here
}
Run Code Online (Sandbox Code Playgroud)
My Wrapper类基本上是一个容器对象,用于获取我的学生列表:
public Class Wrapper {
    private List<Student> students;
    //getters & setters here
}
Run Code Online (Sandbox Code Playgroud)
我一直收到这个错误,"包装"返回null.我不确定缺少什么.有人可以帮忙吗?
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 
    Unrecognized field …Run Code Online (Sandbox Code Playgroud) 我正在使用Jackson JSON库将一些JSON对象转换为Android应用程序上的POJO类.问题是,JSON对象可能会更改并在应用程序发布时添加新字段,但是当添加一个简单的String字段时,它会崩溃,这可以安全地忽略.
有没有办法告诉杰克逊忽略新添加的字段?(例如,POJO对象上不存在)?全球忽视将是伟大的.
我在尝试获取JSON请求并处理它时收到以下错误:
org.codehaus.jackson.map.JsonMappingException:找不到类型[simple type,class com.myweb.ApplesDO]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)
这是我要发送的JSON:
{
  "applesDO" : [
    {
      "apple" : "Green Apple"
    },
    {
      "apple" : "Red Apple"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)
在Controller中,我有以下方法签名:
@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){
    // Method Code
}
Run Code Online (Sandbox Code Playgroud)
AllApplesDO是ApplesDO的包装器:
public class AllApplesDO {
    private List<ApplesDO> applesDO;
    public List<ApplesDO> getApplesDO() {
        return applesDO;
    }
    public void setApplesDO(List<ApplesDO> applesDO) {
        this.applesDO = applesDO;
    }
}
Run Code Online (Sandbox Code Playgroud)
ApplesDO:
public class ApplesDO {
    private String apple;
    public String getApple() {
        return apple;
    }
    public void setApple(String appl) { …Run Code Online (Sandbox Code Playgroud) 在尝试将具有双向关联的JPA对象转换为JSON时,我不断获取
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)
Run Code Online (Sandbox Code Playgroud)
我找到的只是这个线程,基本上建议避免双向关联.有没有人对这个春天的bug有一个解决方法?
------编辑2010-07-24 16:26:22 -------
Codesnippets:
业务对象1:
@Entity
@Table(name = "ta_trainee", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Trainee extends BusinessObject {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "name", nullable = true)
    private String name;
    @Column(name = "surname", nullable = true)
    private String surname;
    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set<BodyStat> bodyStats;
    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, …Run Code Online (Sandbox Code Playgroud) 在JSON中返回空值的首选方法是什么?原语是否有不同的偏好?
例如,如果服务器上的对象有一个名为"myCount"且没有值的Integer,那么该值的最正确的JSON将是:
{}
Run Code Online (Sandbox Code Playgroud)
要么
{
    "myCount": null
}
Run Code Online (Sandbox Code Playgroud)
要么
{
    "myCount": 0
}
Run Code Online (Sandbox Code Playgroud)
字符串的相同问题 - 如果我在服务器上有一个空字符串"myString",则是最好的JSON:
{}
Run Code Online (Sandbox Code Playgroud)
要么
{
    "myString": null
}
Run Code Online (Sandbox Code Playgroud)
要么
{
    "myString": ""
}
Run Code Online (Sandbox Code Playgroud)
或者(上帝帮助我)
{
    "myString": "null"
}
Run Code Online (Sandbox Code Playgroud)
我喜欢集合的约定在JSON中表示为空集合http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html
将显示一个空数组:
{
    "myArray": []
}
Run Code Online (Sandbox Code Playgroud)
编辑摘要
"个人偏好"的观点似乎是现实的,但很短暂,因为社区将消耗更多不同的服务/来源.JSON结构的约定将有助于规范化所述服务的消耗和重用.就建立标准而言,我建议采用大多数杰克逊公约,但有一些例外:
如果要返回一个主要为空值的JSON对象,则可能有一个候选者可以重构为多个服务.
{
    "value1": null,
    "value2": null,
    "text1": null,
    "text2": "hello",
    "intValue": 0, //use primitive only if you are absolutely sure the answer is 0
    "myList": [],
    "myEmptyList": null, //NOT BEST PRACTICE - return [] instead …Run Code Online (Sandbox Code Playgroud) 在搜索了一些现有的JSON库之后,我终于得到了这两个:
我对GSON有点偏爱,但网上的消息是GSon遇到了某种天体性能问题(截至2009年9月).
我继续比较; 与此同时,我正在寻求帮助来决定我的想法.
杰克逊图书馆的ObjectMapper课程似乎是线程安全的.
这是否意味着我应该将我声明ObjectMapper为像这样的静态字段
class Me {
    private static final ObjectMapper mapper = new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud)
而不是像这样的实例级字段?
class Me {
    private final ObjectMapper mapper = new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud) 我有一个发送到服务器和从服务器发送的用户对象.当我发出用户对象时,我不想将散列密码发送给客户端.所以我添加@JsonIgnore了密码属性,但这也阻止了它被反序列化为密码,这使得在没有密码时很难注册用户.
我怎样才能@JsonIgnore应用于序列化而不是反序列化?我正在使用Spring JSONView,所以我没有很多控制权ObjectMapper.
我试过的事情:
@JsonIgnore物业@JsonIgnore仅添加getter方法jackson ×10
java ×9
json ×9
annotations ×2
gson ×2
spring ×2
comparison ×1
data-binding ×1
orm ×1
spring-mvc ×1