i've been fighting json parsing in application , after 3 days of researching still can't figure out issue.
the error being sent "org.json.jsonexception: value "
i got error in try/catch statement i've been working on.
my try/catch looks :
try { // result comes in http request jsonarray jarray = new jsonarray(result); jsonobject jobj = jarray.getjsonobject(0).getjsonobject("results"); string teamname = jobj.getstring("fulltext"); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } the full json i'm getting located here, abridged version follows:
{ "query-continue-offset": 50, "query": { "printrequests": [{ "label": "", "typeid": "_wpg", "mode": 2, "format": false }], "results": { "team:\"die unglaublichen\"": { "printouts": [], "fulltext": "team:\"die unglaublichen\"", "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/team:%22die_unglaublichen%22", "namespace": 822, "exists": true }, "team:(can't stand) le kubb bricks": { "printouts": [], "fulltext": "team:(can't stand) le kubb bricks", "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/team:(can%27t_stand)_le_kubb_bricks", "namespace": 822, "exists": true }, "team:(ohc) kubb team": { "printouts": [], "fulltext": "team:(ohc) kubb team", "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/team:(ohc)_kubb_team", "namespace": 822, "exists": true }, "team:andrewsons3": { "printouts": [], "fulltext": "team:andrewsons3", "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/team:andrewsons3", "namespace": 822, "exists": true } }, "meta": { "hash": "46923025c2d5aac3ee963419db93485d", "count": 50, "offset": 0 } } } this first time seeing json code , honest, it's bit confusing @ first can understand how json works, not how data out of these arrays!
i'm missing something, can't see yet...
the error getting line:
string teamname = jobj.getstring("fulltext"); this because value jobj doesn't have "fulltext" value. instead, field value of each child jsonobject. avoid error, first need check if jsonobject has variable "fulltext":
string teamname = null; if (jobj.has("fulltext")) teamname = jobj.getstring("fulltext"); to simplify json parsing, droidquery provides simple ways create maps , arrays jsonobjects , jsonarrays:
map<string, ?> map = $.map(jobj); (entry<string, ?> entry : map.entryset()) { string teamname = entry.key(); jsonobject json = (jsonobject) entry.value(); map<string, ?> team = $.map(json); string fulltext = (string) team.get("fulltext");//etc }
Comments
Post a Comment