2013-05-11 63 views
6

這可能是一個重複的問題..請參閱此link
我能夠將一個json對象映射到POJO.But如何將json對象的數組轉換爲使用相同jackson框架的pojo。使用傑克遜對象映射器將JSON數組轉換爲POJO

private void jsonToPojo(){ 
    ObjectMapper mapper=new ObjectMapper(); 

    try { 
     User1 user1=mapper.readValue(readFromFile(), User1.class); 
     User1[] user2=mapper.readValue(readFromFile(), User1[].class); 
     System.out.println(user1); 
     Toast.makeText(getApplicationContext(), "inside try", 0).show(); 
    } catch (JsonParseException e) { 
     // TODO Auto-generated catch block 
     Log.i("Exception", "jsonparseexception"); 
     e.printStackTrace(); 
    } catch (JsonMappingException e) { 
     // TODO Auto-generated catch block 
     Log.i("Exception", "jsonmapping exception"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Log.i("Exception", "ioexception"); 
     e.printStackTrace(); 
    } 
} 

這是類用戶對象。

public class User { 
private int age = 23; 
private String name = "amal"; 
private List<String> messages = new ArrayList<String>() { 
    { 
     add("hi"); 
     add("how"); 
     add("are u."); 
    } 
}; 

//getter and setter methods 

@Override 
public String toString() { 
    return "User [age=" + age + ", name=" + name + ", " + 
      "messages=" + messages + "]"; 
} 

這就是我試圖做的:(readFromFile()採用JSON從文件)

User1[] user2=mapper.readValue(readFromFile(), User1[].class); 

的jsonToPojo()爲對象只有一個運作良好。但是,如果我嘗試上面的代碼行,它不是採取以下JSON:

[ 
{ 
    "age":"23", 
    "messages":["hi","how","are u."], 
    "name":"amal" 
}, 

{ 
    "age":"98", 
    "messages":["Reply","my","question"], 
    "name":"You" 
} 
] 
+1

我已經測試過您的示例JSON,您的代碼適用於我。 User和User1類是相同的還是有兩個不同的類? – 2013-05-11 22:35:57

+0

user1是一個新類。以及我想存儲json對象的數組pojo ..多數民衆贊成什麼我無法。 – amalBit 2013-05-13 05:16:15

回答

8

嘗試是這樣的:

public class Test { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    User a = new User(23, "amal"); 
    ArrayList<String> message = new ArrayList<String>(); 
    message.add("m1"); 
    message.add("m2"); 
    a.setMessages(message); 

    User b = new User(58, "pete"); 
    User[] ab = new User[] {a, b}; 

    ObjectMapper mapper = new ObjectMapper(); 

    try { 
     String s1 = getJson1(a); 
     System.out.println(s1); 
     User user1 = mapper.readValue(s1, User.class); 
     System.out.println(user1); 

     System.out.println("----------------"); 

     String s2 = getJson2(ab); 
     System.out.println(s2); 
     User[] user2 = mapper.readValue(s2, User[].class); 
     for (User u : user2) 
      System.out.println(u); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

    private static String getJson1(User user) throws JsonProcessingException { 
     ObjectMapper mapper = new ObjectMapper(); 
     return mapper.writeValueAsString(user); 
    } 

    private static String getJson2(User[] ab) throws JsonProcessingException { 
     ObjectMapper mapper = new ObjectMapper(); 
     return mapper.writeValueAsString(ab); 
    } 

} 

public class User { 
    private int    age; 
    private String   name; 
    private ArrayList<String> messages; 

    public User() { 
     super(); 
    } 

    public User(int age, String name) { 
     this(); 
     this.age = age; 
     this.name = name; 
    } 

    // **************** 
    // Getter & Setter .... 
    // **************** 
} 

你會得到這樣的輸出:

{"age":23,"name":"amal","messages":["m1","m2"]} 
User [age=23, name=amal, messages=[m1, m2]] 
---------------- 
[{"age":23,"name":"amal","messages":["m1","m2"]},{"age":58,"name":"pete","messages":null}] 
User [age=23, name=amal, messages=[m1, m2]] 
User [age=58, name=pete, messages=null] 
+0

這正是我想要的......非常感謝你@Dennis – amalBit 2013-05-13 09:57:33

+0

謝謝,幫助我解決了問題。就像在mapper.readValue中在我的「_user_」類後面添加'[]'一樣簡單。線 – erp 2015-03-12 19:47:19