java - Assistance with JSON from URL -
i trying fetch json url in java, , output results. here json, www.thebluealliance.com/api/v2/match/2015arc_qm1. want match_number, , scores blue , red alliances, , print console. using gson (google json). example, want code return 1, 91, 97...
{ "comp_level": "qm", "match_number": 1, "videos": [], "time_string": null, "set_number": 1, "key": "2015arc_qm1", "time": 1429795800, "score_breakdown": { "blue": { "auto": 0, "foul": 12 }, "red": { "auto": 0, "foul": 0 } }, "alliances": { "blue": { "score": 91, "teams": [ "frc1706", "frc2907", "frc2363" ] }, "red": { "score": 97, "teams": [ "frc2914", "frc360", "frc207" ] } }, "event_key": "2015arc" }
my java code can found below
gson gson = new gson(); string surl = "http://www.thebluealliance.com/api/v2/match/2015arc_qm 1?x-tba-app-id=frc1810:alex-webber:v01"; url url = new url(surl); httpurlconnection request = (httpurlconnection) url.openconnection(); request.connect(); jsonparser jp = new jsonparser(); jsonelement root = jp.parse(new inputstreamreader((inputstream) request .getcontent())); jsonobject rootobj = root.getasjsonobject(); jsonelement results = rootobj .getasjsonobject().get("match_number") .getasjsonobject().getasjsonarray("alliances").get(4) .getasjsonobject().getasjsonarray("blue").getasjsonobject().get("score"); string match = results.getasstring();
try out, did not test it, see in json document, have couple of issues in code, alliances not json array, blue , red not json arrays, json objects based on see in json document,
jsonobject rootobj = root.getasjsonobject(); jsonelement match_number = rootobj.get("match_number"); jsonobject alliances = rootobj.getasjsonobject("alliances"); jsonelement blue = alliances.getasjsonobject("blue").get("score"); jsonelement red = alliances.getasjsonobject("red").get("score"); system.out.println(match_number.getasstring()+","+blue.getasstring()+","+red.getasstring());
Comments
Post a Comment