2013-05-11 16 views
6

Questa è probabilmente una domanda doppia. Si prega di fare riferimento a questo link.
Sono in grado di mappare un oggetto json su POJO. Ma come posso convertire l'array di oggetti json in pojo utilizzando lo stesso framework di jackson.Conversione di array JSON in POJO utilizzando il mappatore oggetti jackson

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(); 
    } 
} 

Questo è l'oggetto utente della classe.

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 + "]"; 
} 

Questo è quello che ho cercato di fare: (ReadFromFile() si è JSON da un file)

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

Il jsonToPojo() sta lavorando bene per un solo oggetto. Tuttavia, se provo la riga di codice precedente, non è che segue JSON:

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

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

I ho testato il tuo esempio JSON e il tuo codice funziona per me. Le classi User e User1 sono uguali o hai due classi diverse? –

+0

user1 è una nuova classe. beh voglio archiviare la serie di oggetti json nel pojo .. questo è quello che non sono in grado di fare. – amalBit

risposta

8

provare qualcosa di simile:

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 .... 
    // **************** 
} 

Otterrete questa uscita:

{"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

Questo è esattamente quello che volevo ... grazie mille @ Dennis – amalBit

+0

Grazie, mi ha aiutato anche io .. Era semplice come aggiungere il '[]' dietro la mia classe "_user_" nel 'mapper.readValue. ..' line – erp

Problemi correlati