2012-11-28 41 views
2

我有一個JSON:無法與傑克遜反序列化JSON的lib

{ 
    "firstField": "Something One", 
    "secondField": "Something Two", 
    "thirdField": [ 
     { 
      "thirdField_one": "Something Four", 
      "thirdField_two": "Something Five" 
     }, 
     { 
      "thirdField_one": "Something Six", 
      "thirdField_two": "Something Seven" 
     } 
    ], 
    "fifthField": [ 
     { 
      "fifthField_one": "Something… ", 
      "fifthField_two": "Something...", 
      "fifthField_three": 12345 
     }, 
     { 
      "fifthField_one": "Something", 
      "fifthField_two": "Something", 
      "fifthField_three": 12345 
     } 
    ] 
} 

我有我的課:

public static class MyClass { 
     @JsonProperty 
     private String firstField, secondField; 
     @JsonProperty 
     private ThirdField thirdField; 
     @JsonProperty 
     private FifthField fifthField; 

     public static class ThirdField { 
      private List<ThirdFieldItem> thirdField; 
     } 

     public static class ThirdFieldItem { 
      private String thirdField_one, thirdField_two; 
     } 

     public static class FifthField { 
      private List<FifthFieldItem> fifthField; 
     } 

     public static class FifthFieldItem { 
      private String fifthField_one, fifthField_two; 
      private int fifthField_three; 
     } 
    } 

我與傑克遜庫反序列化他們:

public void testJackson() throws IOException { 
    JsonFactory factory = new JsonFactory(); 
    ObjectMapper mapper = new ObjectMapper(factory); 
    File from = new File("text.txt"); // JSON I mentioned above 
    mapper.readValue(from, MyClass.class); 
} 

但我得到例外:

org.codehaus.jackson.map.JsonMappingException:無法反序列化主要$ MyClass的$ ThirdField的 實例出來START_ARRAY令牌

回答

5

您已定義thirdFieldfifthField屬性,如您的JSON陣列。他們需要的是在你的Java Bean數組或集合,以及:

public static class MyClass { 
    @JsonProperty 
    private String firstField, secondField; 

    @JsonProperty 
    private Collection<ThirdField> thirdField; 

    @JsonProperty 
    private Collection<FifthField> fifthField; 

    /// ... 
} 

當你正在經歷以及將現有的JSON對象爲豆類,記住,JSON數據是非常喜歡的地圖。如果您設想如何將地圖中的數據映射到對象中,它確實有所幫助。您的ThirdFieldFifthField對象需要映射JSON中的定義。這是你的JSON說什麼ThirdField是:

{ 
    "thirdField_one": "Something Four", 
    "thirdField_two": "Something Five" 
} 

字面轉換,爲一個Java bean爲您提供:

public class ThirdField implements Serializable { 
    private String thirdField_one; 
    private String thirdField_two; 

    // ... 
} 

您可以在註釋等添加等,以得到一個完整成熟的豆。爲你的FifthField對象做同樣的事情。

0

備註:我是EclipseLink JAXB (MOXy)的領導者和JAXB (JSR-222)專家組的成員。

由於它不一定總能改變你的域模型(如answered by @Perception),下面是你如何使用MOXy將原始對象模型映射到所需的JSON。

Java模型

在該使用情況,您可以利用@XmlPath(".")擴展。這告訴MOXy將目標對象的內容放入源節點。

@XmlAccessorType(XmlAccessType.FIELD) 
public static class MyClass { 
    private String firstField, secondField; 

    @XmlPath(".") 
    private ThirdField thirdField; 

    @XmlPath(".") 
    private FifthField fifthField; 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class ThirdField { 
     private List<ThirdFieldItem> thirdField; 
    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class ThirdFieldItem { 
     private String thirdField_one, thirdField_two; 
    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class FifthField { 
     private List<FifthFieldItem> fifthField; 
    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class FifthFieldItem { 
     private String fifthField_one, fifthField_two; 
     private int fifthField_three; 
    } 
} 

轉換碼

演示下面的代碼說明如何啓用莫西的JSON綁定。

public static void main(String[] args) throws Exception { 
    Map<String, Object> properties = new HashMap<String, Object>(2); 
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); 
    properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); 
    JAXBContext jc = JAXBContext.newInstance(new Class[] {MyClass.class}, properties); 

    Unmarshaller unmarshaller = jc.createUnmarshaller(); 
    StreamSource json = new StreamSource("src/forum13600952/input.json"); 
    MyClass myClass = unmarshaller.unmarshal(json, MyClass.class).getValue(); 

    Marshaller marshaller = jc.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    marshaller.marshal(myClass, System.out); 
} 

input.json /輸出

Belos是從你的問題稍微重新格式化,以通過MOXY產生的輸出匹配輸入。

{ 
    "firstField" : "Something One", 
    "secondField" : "Something Two", 
    "thirdField" : [ { 
     "thirdField_one" : "Something Four", 
     "thirdField_two" : "Something Five" 
    }, { 
     "thirdField_one" : "Something Six", 
     "thirdField_two" : "Something Seven" 
    } ], 
    "fifthField" : [ { 
     "fifthField_one" : "Something...", 
     "fifthField_two" : "Something...", 
     "fifthField_three" : 12345 
    }, { 
     "fifthField_one" : "Something", 
     "fifthField_two" : "Something", 
     "fifthField_three" : 12345 
    } ] 
} 

更多信息