java - JSON deserialization with Gson -


this json data:

{   "boards": [     {         "board": "3",         "title": "3dcg",         "ws_board": 1,         "per_page": 15,         "pages": 11     },     {         "board": "a",         "title": "anime & manga",         "ws_board": 1,         "per_page": 15,         "pages": 11     },     {         "board": "adv",         "title": "advice",         "ws_board": 1,         "per_page": 15,         "pages": 11     },     ...   ] } 

this code deserialization:

jsonobject json = readjsonfromurl("http://api.4chan.org/boards.json"); string jsonboards = json.tostring(); gson gson = new gson(); map<string, object> map = new hashmap<string,object>(); map = (map<string,object>) gson.fromjson(jsonboards, map.getclass()); 

but doesn't job done. need way boards name, title, , information each board. when use map.get(); key "boards" want map of every board , title , on.

you need class structure maps json data. don't need use map anywhere, since json not represent map!

in fact represents object {...} contains field called "boards", in turn represents array [...] of objects {...}...

so class structure matches json (in pseudo-code):

class response   list<board> boards  class board   string board   string title   int ws_board   int per_page   int pages 

then, in order parse json class structure, need this:

gson gson = new gson(); response response = gson.fromjson(jsonboards, response.class); 

so you'll have data of boards into:

string namei = response.getboards().get(i).getname(); string titlei = response.getboards().get(i).gettitle(); //and on... 

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 -