desidero serializzare questo oggetto JSON StringSerialize oggetto Java con GSON
public class Person {
public String id;
public String name;
public Person parent;
}
e ottenere un risultato simile:
{id: 1, name: "Joe", parent: 2}
Ho provato ad utilizzare
Person p = new Person(1, "Joe", new Person(2, "Mike"));
Gson gson = new GsonBuilder()
.registerTypeAdapter(Persona.class, new PersonSerializer()).create();
String str = gson.toJson(p);
ma invece, ho ottenuto:
"1"
PersonSerializer:
public class PersonSerializer implements JsonSerializer<Person> {
public JsonElement serialize(Person src, Type typeOfSrc, ...) {
return new JsonPrimitive(src.id);
}
}
prega ogni suggerimento è benvenuto
Grazie, Mario
http://stackoverflow.com/q/7085545/1815624 e http://stackoverflow.com/q/10067441/1815624 sono stati legati nel mio caso, ma è stato http://stackoverflow.com/questions/10067441/gson-converts-to-linkedhashmap-instead-of-my-object#comment62206167_33561747 che lo ha risolto – CrandellWS