2012-07-17 44 views
2

我使用Jackson objectMapper来解析JSON字符串。我分配了JSON一些对象RuleModel,其中杰克逊objectMapping没有得到JSON数据

的JSON是

"{'ruleId': 1000000, 
Formula': { 
    'ruleAggregates': 'foo', 
    'fields': ['foo', 'foo'], 
    'Children':[{ 
     'Formula': 
      {'ruleAggregates': 'a', 
       'fields': ['1', '2'], 
       'Children': []}}, 
     { 'Formula': 
       {'ruleAggregates': 'b', 
       'fields': ['3', '4'], 
       'Children': []}}, 
     {} 
    ]}}", 

和Java模型是

RuleModel{ 
private long ruleId; 
private Formula formula; 
} 

和配方是

Formula{ 
private String ruleAggregates 
private List<String> fields; 
private List<FormulaModel> Children; 
} 

我可以得到ruleId值和ruleAggregates值为第一个ruleAggregates,但是当我尝试进入Children时,它得到公式但不是 里面的值因此,当我从儿童获得任何价值时,我得到空值

回答

6

以下是从原来的问题反序列化JSON的例子(修正必要的有效性)。这个例子还演示了如何配置Jackson以允许使用单引号的JSON元素。

从原来的问题,我不明白具体问题与尝试反序列化JSON的具体问题。对于简单的数据绑定,请注意,Java属性名称必须与JSON元素名称匹配,并且Java数据结构必须与JSON数据结构匹配。

input.json

{ 
    'ruleId': 1000000, 
    'Formula': 
    { 
     'ruleAggregates': 'foo', 
     'fields': ['foo', 'foo'], 
     'Children': 
     [ 
      { 
       'Formula': 
       { 
        'ruleAggregates': 'a', 
        'fields': ['1', '2'], 
        'Children': [] 
       } 
      }, 
      { 
       'Formula': 
       { 
        'ruleAggregates': 'b', 
        'fields': ['3', '4'], 
        'Children': [] 
       } 
      }, 
      {} 
     ] 
    } 
} 

Java对象模型

import com.fasterxml.jackson.annotation.JsonProperty; 

class RuleModel 
{ 
    private long ruleId; 
    @JsonProperty("Formula") private Formula formula; 
} 

class Formula 
{ 
    private String ruleAggregates; 
    private List<String> fields; 
    private List<FormulaModel> Children; 
} 

class FormulaModel 
{ 
    @JsonProperty("Formula") private Formula formula; 
} 

JacksonFoo.java

import java.io.File; 
import java.util.List; 

import com.fasterxml.jackson.annotation.PropertyAccessor; 
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; 
import com.fasterxml.jackson.core.JsonParser; 
import com.fasterxml.jackson.databind.ObjectMapper; 

public class JacksonFoo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); 
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); 

    RuleModel model = mapper.readValue(new File("input.json"), RuleModel.class); 
    System.out.println(mapper.writeValueAsString(model)); 
    } 
} 

输出:

{ 
    "ruleId": 1000000, 
    "Formula": { 
     "ruleAggregates": "foo", 
     "fields": [ 
      "foo", 
      "foo" 
     ], 
     "Children": [ 
      { 
       "Formula": { 
        "ruleAggregates": "a", 
        "fields": [ 
         "1", 
         "2" 
        ], 
        "Children": [] 
       } 
      }, 
      { 
       "Formula": { 
        "ruleAggregates": "b", 
        "fields": [ 
         "3", 
         "4" 
        ], 
        "Children": [] 
       } 
      }, 
      { 
       "Formula": null 
      } 
     ] 
    } 
} 
0

奇尔德伦以大写字母C开头,杰克逊如果我没有误会杰克逊的默认行为是骆驼大小写。换句话说,杰克逊搜索'childeren'。您可以使用此字段注释覆盖属性名称。

@JsonProperty("Children") 
private List<FormulaModel> Children; 
+0

使用和不断变化的儿童,以降低情况c试过了,没有工作,为ruleAggregates代替“A”和“B” – yangdafish 2012-07-17 22:19:06

+0

humh很奇怪,我会低级别的调试仍然得到空,我不知道为什么这不起作用(有或没有注释) – 2012-07-18 07:32:33

0

在JSON: 使用字段名双引号; 以小写字母开头字段名称;

在Java中: 为字段添加getter和setter方法; 实现java.io.Serializable可能会有所帮助;

你也可以使用一个在线JSON验证工具,如http://jsonlint.com/