java - deserialize JSON data for different types of objects -
im using gson on android device.
i have json data coming in, can come in form of few different objects.
this how think need handle it.
public class command { public string command; } string json = {"command":"something", "date":"now"} string command = gson.fromjson(message, command.class);
then switch on command
switch(command) { case: //deserialize "something" object; break; case: other somthing //deserialize "other somthing" object; break; case: object 3 //deserialize "object 3" object; break; }
does gson have sort of auto mapping best suited object, dont have make custom object handler , deseraialize string twice?
i parse general jsonobject using
jsonparser parser = new jsonparser(); jsonobject jsonobject = parser.parse(json).getasjsonobject();
then find unique each json schema , depending on schema convert bean using
gson.fromjson(jsonobject, appropriatebean.class);
Comments
Post a Comment