java - JSON has unique keys for all objects, how to convert to POJO using GSON? -
this question has answer here:
i have particular situation retrieving list of objects have unique ids keys, , have nested objects have own unique ids.
i using gson
create pojos when creating model classes these objects need serialize unique key values, not solution.
i have attached image give better idea of talking about:
i want gson
create list of nestedobject1
self contain list of nestedobject2
. problem key value unique each type of nestedobject
i thinking of doing so:
objects.java
public class objects { @serializedname("objects") private list<nestedobject1> mnestedobjects1; public list<nestedobject1> getnestedobjects1() { return mnestedobjects1; } }
nestedobjects1.java
public class nestedobject1 { @serializedname("what-to-put-here?") private string mid; public string getid() { return mid; } @serializedname("what-to-put-here?") private list<nestedobject2> mnestedobjects2; public list<nestedobject2> getnestedobjects2() { return mnestedobjects2; } }
nestedobject2
public class nestedobject2 { // same deal here? }
any suggestions on how tackle this?
edit: sample json output
"objects" : { "-jrevnezhqv73jxku0-u" : { "-jrevobezaysrrttngp-" : { "started" : true }, "-jrevqanythhjrimgr-r" : { "started" : true } }, "-jrf0_ywy_nmrmrob3qp" : { "-jrf0lwrsa5kmu6snc7z" : { "started" : true }, "-jrf1txlofd2w4wn8q4n" : { "started" : true }, "-jrf2skhdmmt9wn3wkvs" : { "started" : true } }, "-jrf3xskpvo2wmzf1osb" : { "-jrf3zywyxuz0rdoovna" : { "started" : true }, "-jrf4d-ceslhqbbzdft1" : { "started" : true }, "-jrf5gufddqfgjw8ayrb" : { "started" : true } }, }
as names not same, can't use neither annotation or have field same name in java class.
what suggest implement custom deserializer shouldn't have care these key-values.
assuming proper constructors here's how implement deserializer objects
class (by way it's bad name there class named objects
in jdk).
class objectsdeserializer implements jsondeserializer<objects> { @override public objects deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { jsonobject jsonobject = json.getasjsonobject().getasjsonobject("objects"); list<nestedobject1> list = new arraylist<>(); for(map.entry<string, jsonelement> entry : jsonobject.entryset()) { list<nestedobject2> nestedobject2list = new arraylist<>(); //here deserialize each nestedobject2 , put in list list.add(new nestedobject1(entry.getkey(), nestedobject2list)); } return new objects(list); } }
then register deserializers in gson parser:
gson gson = new gsonbuilder() .registertypeadapter(objects.class, new objectsdeserializer()) .registertypeadapter(nestedobject2.class, new nestedobject2deserializer()) .create(); objects o = gson.fromjson(new filereader(new file("file.json")), objects.class);
which outputs:
objects{mnestedobjects1= nestedobject1{mid='-jrevnezhqv73jxku0-u', mnestedobjects2=[]} nestedobject1{mid='-jrf0_ywy_nmrmrob3qp', mnestedobjects2=[]} nestedobject1{mid='-jrf3xskpvo2wmzf1osb', mnestedobjects2=[]} }
you'd need define custom deserializer nestedobject2
class, here fill empty list, guess able it's same principle.
hope helps! :-)
Comments
Post a Comment