Deserializing 2D array of objects JSON to Java -
I am using GSON to deserialize my JSON string. It seems that when you try to deserialize the 2D array of objects, tickles here are bit:
{. "State": ["Span": "{{" name ":" STATEA "," duration ": 4}, {" name ":" STATEB "," duration ": 4}], {" ALIVEPHASE ": [{" Name ":" STATEA "," Duration ": 5}, {" Name ":" STATEB "," Duration ": 4}]}]} A part of a larger JSON above is that I got the expected BEGIN_ARRAY but was BEGIN_OBJECT , I have seen my JSON, but I do not know where it is wrong
Group g = gson.fromJson (configfile, Group.class); I use this to start reading. Is it not enough to take action on string for 2D array?
I tried both in Group.class
state state [] [] states; and
public list & lt; List & lt; State & gt; & Gt; States; Everything else seems to recover.
Do I have to use it (below)?
Type type archive = new typography & lt; Collection & lt; Integer & gt; & Gt; () {} GetType (); Collections & lt; Integer & gt; Ints2 = gson.from Jason (json, collection type);Your displayed JSON has a field
state.In that field there is an array that you show as two different types of objects one is in a field
SPAWNINGPHASE, another field < Code> ALIVEPHASE . Each of them holds an orange that has another type of object.This is a problem for automatic self-ownership of a POJO.
The only way could possibly work if there were areas for each "step" of your
stateclass (It is assumed that you have a Java class whose name is < Code> NameAndDuration is you can map those internal objects).class state {list & lt; NameAndDuration & gt; SPAWNINGPHASE; & Lt; NameAndDuration & gt; ALIVEPHASE; ...}provided that you mean that your
groupclass represents JSON object, this will be:< Code> list & lt; State & gt; States;The GSO has quietly ignored any missing fields in JSON, so you can list only the
listof yourlistWith one of the field sets and all othernullAs you must:
public list & lt; Maps & lt; String, list & lt; NameAndDuration & gt; & Gt; & Gt; States;A JSON object is naturally a key / value map and because the objects inside the array are of a normal type, which will work. You have a
listwith amapin which each one had an entry, the "step" as the key, and the list of those internal objects. Not quite right, but it will work.Option B and anything else that is more understandable is changing JSON, so the "step" is an value :
<">" "" [ "" Span "," Value ": [{" Name ":" STATEA "," Period ": 4}, {" Name ":" STATEB "," Duration ": 4}]}]}and change your
stateclass accordingly:class state {String status; list & gt; values & gt; values; }(Note that you can also use
enumforstephere)now
List & lt; state & gt; states;little (It seems to be a bit of a misnomer, it seems that in fact you have a list of "steps" which state information "list of states" Instead) is
Comments
Post a Comment