android - How to handle my JSON string from Google Sheets API v4 in Java -


i'm new google sheets api , seems can't make progress.

i have google spreadsheet , i'm using api url json data:

https://sheets.googleapis.com/v4/spreadsheets/spreadsheetid/values/01jul2017!a1:t?key=apikey 

this end giving me this:

{   "range": "'01jul2017'!a1:t2828",   "majordimension": "rows",   "values": [     [       "name",       "school",       "subschool",       "descriptor"     ],     [       "acid arrow",       "conjuration",       "creation",       "acid"     ],     [       "air walk",       "transmutation",       "",       "air"     ] ] } 

now problem can't find how handle data. i'm used reading json data keys (e.g. "school":"conjuration") can use following:

spell.setschool(jsonobjectspell.getstring("school"));  

can me out or put me in right direction?

you can convert json string json tree , iterate on of nodes , set values. how convert tree depends on json library using. below example using jackson 2.

objectmapper mapper = new objectmapper(); jsonnode jsontree = mapper.readtree(json);  jsonnode valuesarray = jsontree.get("values");  // each array in values (jsonnode valuesitemarray : valuesarray) {     // each sub-array     (jsonnode valuesitem : valuesitemarray) {         // valuesitem name, school, etc..     } } 

you can use custom deserializer values field don't have handle of json, deserializer same thing above.

edit

if can assume first array name, school, etc. first , in right order, can below.

objectmapper mapper = new objectmapper(); jsonnode jsontree = mapper.readtree(json);  arraynode valuesarray = (arraynode) jsontree.get("values");  list<map<string, string>> spells = new arraylist<map<string, string>>(); map<string, string> spellmap = null;  (int = 1; < valuesarray.size(); i++) {     arraynode valuesitemarray = (arraynode) valuesarray.get(i);      spellmap = new hashmap<string, string>();     spellmap.put("name", valuesitemarray.get(0).astext());     spellmap.put("school", valuesitemarray.get(1).astext());     spellmap.put("subschool", valuesitemarray.get(2).astext());     spellmap.put("descriptor", valuesitemarray.get(3).astext());      spells.add(spellmap); } 

then can iterate on list , value using .get("name") or .get("school") on each map.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -