Ошибка в org.json.JSON.typeMismatch(JSON.java:100)
На самом деле я пытаюсь получить данные из YQL, и я в этом преуспел, но когда дело касается сложных объектов / массивов json, отладчик показывает мне эту ошибку
в org.json.JSON.typeMismatch(JSON.java:100)
Ссылка на JSON, если не происходит живое совпадение, это код JSON
{
"query": {
"count": 1,
"created": "2017-04-07T20:16:20Z",
"lang": "en-US",
"diagnostics": {
"cache": {
"execution-start-time": "0",
"execution-stop-time": "2",
"execution-time": "2",
"method": "GET",
"type": "MEMCACHED",
"content": "TABLE.queryyahooapiscomproductionsg3.cricket.scorecard.live.summary.4daacfcfafc467783e90b0c5c31a615f"
},
"publiclyCallable": "true",
"url": {
"execution-start-time": "2",
"execution-stop-time": "5",
"execution-time": "3",
"content": "http://cricketwebservice/ws/cwsapi/live/livematches.php?output=summary"
},
"user-time": "5",
"service-time": "5",
"build-version": "2.0.97"
},
"results": {
"Scorecard": {
"v": "0",
"mid": "196000",
"m": "2",
"series": {
"series_id": "12604",
"series_name": "Pakistan in West Indies ODI Series, 3 ODI Cricket Series, 2017"
},
"ecf": "0",
"place": {
"vid": "179",
"stadium": "Providence Stadium, Guyana",
"city": "Guyana",
"country": "West Indies",
"Gimaget": "https://s.yimg.com/qx/cricket/fufp/images/venue_179_thumb-12-7-2010-89c7b8de5692a58c7abcd56bd61fdfeb.jpg",
"date": "20170407133000",
"enddate": "20170407213000"
},
"mn": "1st ODI",
"teams": [
{
"i": "9",
"fn": "West Indies",
"sn": "WI",
"logo": {
"std": "https://s.yimg.com/qx/cricket/fufp/images/west_indies-26-2-2013-d3ee3026b84398864c10e4320494c6e8.jpg",
"small": "https://s.yimg.com/qx/cricket/fufp/images/west_indies_small-26-2-2013-d0e88ec3179f31a2c0ebc05e1830453e.jpg"
},
"flag": {
"std": "https://s.yimg.com/qx/cricket/fufp/images/west_indies_flag-1-2-2011-1f60ba1dc32a680e11ae5efe99bcc924.gif",
"small": "https://s.yimg.com/qx/cricket/fufp/images/west_indies_flag_small-1-2-2011-7f81c93235b1ea332d460c28778e21a4.gif",
"roundstd": "https://s.yimg.com/qx/cricket/fufp/images/9-11-2-2012-c51c1d9604f42152a6e87e5cb38542e3.png",
"roundsmall": "https://s.yimg.com/qx/cricket/fufp/images/9_s-11-2-2012-b317c02e8201f9cbbefd3d21381bb534.png",
"roundlarge": "https://s.yimg.com/qx/cricket/fufp/images/9_l-11-2-2012-da7e8c6fe82f0fa6ec7ee4d3051d3ba6.png"
}
},
{
"i": "6",
"fn": "Pakistan",
"sn": "PAK",
"logo": {
"std": "https://s.yimg.com/qx/cricket/fufp/images/pakistan-26-2-2013-f6f54d67dea9677b03ddc1c009c070f9.jpg",
"small": "https://s.yimg.com/qx/cricket/fufp/images/pakistan_small-26-2-2013-6af95f1da13f29b8ef5ccfbf3cb89648.jpg"
},
"flag": {
"std": "https://s.yimg.com/qx/cricket/fufp/images/pakistan_flag-1-2-2011-f99a8132f62ed22d64b985c2ba0d65eb.gif",
"small": "https://s.yimg.com/qx/cricket/fufp/images/pakistan_flag_small-1-2-2011-5bbc7ebe24b9bc2460e131bfc9cafd78.gif",
"roundstd": "https://s.yimg.com/qx/cricket/fufp/images/6-11-2-2012-e492bb0be566324796f25d3bcd2c3019.png",
"roundsmall": "https://s.yimg.com/qx/cricket/fufp/images/6_s-11-2-2012-2ef4be67daf68f843f48115ff5ba8f1a.png",
"roundlarge": "https://s.yimg.com/qx/cricket/fufp/images/6_l-11-2-2012-472fc93551b80e5564b9fd0355191a4e.png"
}
}
],
"past_ings": [
{
"s": {
"t": "1",
"i": "2",
"ps": "0",
"stay_live": "Yes",
"ld": "No",
"ldmsg": null,
"pms": "live",
"ed": "Power Play 2",
"a": {
"fo": "0",
"pp": "2",
"i": "9",
"cr": "4.69",
"r": "158",
"o": "33.4",
"w": "4",
"b": "0",
"lb": "5",
"wd": "12",
"nb": "1",
"pt": "0",
"l": "0",
"rr": "9.24",
"ru": "151",
"ro": "16.2",
"tg": "309",
"cp": {
"cp": "2",
"bls": "9",
"s": "1"
}
},
"tz": "-04:00"
},
"d": {
"a": {
"t": {
"i": "11705",
"a": "3",
"c": "Batting",
"dt": null,
"fd": "0",
"bd": "0",
"cb": "no",
"b": "4",
"r": "1",
"sr": "25",
"four": "0",
"six": "0",
"name": "Jason Mohammed"
}
}
}
},
{
"s": {
"t": "1",
"i": "1",
"ps": "0",
"stay_live": "Yes",
"ld": "No",
"ldmsg": null,
"pms": "live",
"ed": "Power Play 3",
"a": {
"fo": "0",
"pp": "3",
"i": "6",
"cr": "6.16",
"r": "308",
"o": "50",
"w": "5",
"b": "3",
"lb": "3",
"wd": "7",
"nb": "1",
"pt": "0",
"l": "0"
},
"tz": "-04:00"
}
}
],
"toss": {
"win": "9",
"bat": "0",
"text": "xxx-xxx-xxx"
},
"result": "0",
"ms": "Drinks",
"mt": "2",
"cts": "1491596158"
}
}
}
}
и ниже код, который я пробовал до сих пор.
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject query = jsonObject.getJSONObject("query");
JSONObject results = query.getJSONObject("results");
JSONArray Scorecard = results.getJSONArray("Scorecard");
for (int i = 0; i < Scorecard.length(); i++) {
JSONObject e = Scorecard.getJSONObject(i);
JSONArray past_ings = new JSONArray(e.getString("past_ings"));
for (int j = 0; j < past_ings.length(); j++) {
JSONObject f = past_ings.getJSONObject(j);
JSONObject getter = new JSONObject(f.getString("s"));
team = getter.getString("t");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
но теперь я хочу получить доступ к объекту "а" в "с"
так где я не так делаю?
PS: приведенный выше код работает.
1 ответ
Прежде всего, Scorecard не является массивом. Я изменил ваш код и протестировал с вашим JSON. Оно работает.
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject query = jsonObject.getJSONObject("query");
JSONObject results = query.getJSONObject("results");
JSONObject Scorecard = results.getJSONObject("Scorecard");
JSONArray past_ings = Scorecard.getJSONArray("past_ings");
for (int j = 0; j < past_ings.length(); j++) {
JSONObject f = past_ings.getJSONObject(j);
JSONObject getter = new JSONObject(f.getString("s"));
JSONObject a = getter.getJSONObject("a");
}
} catch (JSONException e) {
e.printStackTrace();
}