Jackson objectMapping没有获得JSON数据

时间:2012-07-17 21:42:26

标签: java json recursion jackson

我正在使用Jackson objectMapper来解析JSON字符串。我将JSON分配给某个对象RuleModel,其中

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是

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

我可以获取ruleId值,并且ruleAggregates第一个ruleAggregates的值,但是当我尝试进入Children时,它获取公式而不是里面的值 因此,当我从孩子那里获得任何价值时,我得到空值

3 个答案:

答案 0 :(得分: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
            }
        ]
    }
}

答案 1 :(得分:0)

Childeren以大写字母C,杰克逊开头如果我没有弄错杰克逊的默认行为是骆驼案。换句话说,杰克逊搜索'childeren'。您可以使用此字段批注覆盖属性名称。

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

答案 2 :(得分:0)

在JSON中: 对字段名称使用双引号; 用小写字母开始字段名称;

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

您还可以使用在线json验证工具,例如http://jsonlint.com/