java - Deserialization with gson when JSON no have variable names -


i have json bellow:

{     "0001":[111, "blabla", "lala", "kkk",80,20],     "002":[222, "blabla", "lala", "kkk",80,40],     "003":[333, "blabla", "lala", "kkk",100,20],     "000":[444, "blabla", "lala", "kkk",800,60],     "555":[555, "blabla", "lala", "kkk",80,20,     "100":[48, "blabla", "lala", "kkk",80,20] } 

i'm having trouble gson deserialize. know work json have variable names defined below:

{    "item":"001":["id":1,"description":"bla bla"],    "item":"002":["id":2,"description":"bla bla"] } 

then defined class same variable names of json , execute parse

public class dataclass {   string item;   int id;   string description;    getters , setters;  }  gson.fromjson (json, dataclass.class); 

but not know how in case need use json without variable names (in beginning of post). can help?

this class answers question:

package stackoverflow.questions.q18116805;  import java.lang.reflect.type; import java.util.*;  import com.google.common.reflect.typetoken; import com.google.gson.gson;  public class q18116805 {       public static void main(string[] args) {         string json =                                                              "{  \"0001\":[111, \"blabla\", \"lala\", \"kkk\",80,20],   "+             "    \"002\":[222, \"blabla\", \"lala\", \"kkk\",80,40],   "+             "    \"003\":[333, \"blabla\", \"lala\", \"kkk\",100,20],  "+             "    \"000\":[444, \"blabla\", \"lala\", \"kkk\",800,60],  "+             "    \"555\":[555, \"blabla\", \"lala\", \"kkk\",80,20],   "+             "    \"100\":[48, \"blabla\", \"lala\", \"kkk\",80,20]}";          type atype = new typetoken<map<string,arraylist<object>>>() {}.gettype();         map<string,arraylist<object>> map = new gson().fromjson(json, atype);          system.out.println(map);     } } 

this result:

{0001=[111.0, blabla, lala, kkk, 80.0, 20.0], 002=[222.0, blabla, lala, kkk, 80.0, 40.0], 003=[333.0, blabla, lala, kkk, 100.0, 20.0], 000=[444.0, blabla, lala, kkk, 800.0, 60.0], 555=[555.0, blabla, lala, kkk, 80.0, 20.0], 100=[48.0, blabla, lala, kkk, 80.0, 20.0]}


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -