D'abord, désolé pour mon mauvais anglais.Convertir l'objet incorrect de JSON
Deuxièmement, mon problème.
J'essaie de convertir en JSON et sauvegarder cette structure:
class Revision{
private String auth;
private HashMap<String, List<HashMap<String, Object>>> rev;
public String getAuth(){
return auth;
}
public HashMap<String, List<HashMap<String, Object>>> getRev(){
return rev;
}
public void setAuth(String auth){
this.auth = auth;
}
public void setRev(HashMap<String, List<HashMap<String, Object>>> rev){
this.rev = (HashMap<String, List<HashMap<String, Object>>>) rev.clone();
}
public String toString(){
return "Auth: " + auth + ", rev: " + rev;
}
}
je le fais avec ce code:
public static void main (String[] argc){
Gson gson = new Gson();
Revision revision = new Revision();
HashMap<String, List<HashMap<String, Object>>> HM = new HashMap<String, List<HashMap<String, Object>>>();
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> HMin = new HashMap<String, Object>();
HMin.put("id", 12);
HMin.put("type", "toster");
list.add(HMin);
HM.put("mark", list);
revision.setRev(HM);
revision.setAuth("ololo");
String json = gson.toJson(revision);
Revision test = new Gson().fromJson(json, Revision.class);
System.out.println(json);
System.out.println(revision);
System.out.println(test);
}
Dans enfin j'obtenir ce résultat:
{"auth":"ololo","rev":{"mark":[{"id":12,"type":"toster"}]}}
Auth: ololo, rev: {mark=[{id=12, type=toster}]}
Auth: ololo, rev: {mark=[{[email protected], [email protected]}]}
Comme vous pouvez voir, après la conversion, les paramètres de type d'objet incorrects.
S'il vous plaît, pouvez-vous me dire comment résoudre ce problème?
Merci d'avance!
Problème de méthode toString()? – vstoyanov
Je ne pense pas. J'essaie de le faire: HashMap>> HMout = new HashMap >>(); HMout = (HashMap >>) test.getRev(). Clone(); System.out.println (((HMout.get ("mark")). Get (0)). Get ("id"). ToString()); Et j'obtiens ** [email protected]** comme résultat = ( –
JackTheCat