2012-07-17 14 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/