i'm trying to parse a json string to a JSONArray element, but when i try i get "cannot be converted to JSONArray"
My string is this way (but way longer):
{
"mylist": {
"myinfo": {
"user_id": 6225804,
"user_name": "culo",
"user_watching": 1092,
"user_completed": 0,
"user_onhold": 0,
"user_dropped": 0,
"user_plantowatch": 0,
"user_days_spent_watching": 0
},
"anime": [{
"series_animedb_id": 1,
"series_title": "Cowboy Bebop",
"series_synonyms": "; Cowboy Bebop",
"series_type": 1,
"series_episodes": 26,
"series_status": 2,
"series_start": "1998-04-03",
"series_end": "1999-04-24",
"series_image": "https:\/\/myanimelist.cdn-dena.com\/images\/anime\/4\/19644.webp",
"my_id": 0,
"my_watched_episodes": 0,
"my_start_date": "0000-00-00",
"my_finish_date": "0000-00-00",
"my_score": 0,
"my_status": 1,
"my_rewatching": 0,
"my_rewatching_ep": 0,
"my_last_updated": 1493924579,
"my_tags": ""
}, {
"series_animedb_id": 5,
"series_title": "Cowboy Bebop: Tengoku no Tobira",
"series_synonyms": "Cowboy Bebop: Knockin' on Heaven's Door; Cowboy Bebop: The Movie",
"series_type": 3,
"series_episodes": 1,
"series_status": 2,
"series_start": "2001-09-01",
"series_end": "2001-09-01",
"series_image": "https:\/\/myanimelist.cdn-dena.com\/images\/anime\/6\/14331.webp",
"my_id": 0,
"my_watched_episodes": 0,
"my_start_date": "0000-00-00",
"my_finish_date": "0000-00-00",
"my_score": 0,
"my_status": 1,
"my_rewatching": 0,
"my_rewatching_ep": 0,
"my_last_updated": 1496668154,
"my_tags": ""
}, {
"series_animedb_id": 6,
"series_title": "Trigun",
"series_synonyms": "; Trigun",
"series_type": 1,
"series_episodes": 26,
"series_status": 2,
"series_start": "1998-04-01",
"series_end": "1998-09-30",
"series_image": "https:\/\/myanimelist.cdn-dena.com\/images\/anime\/7\/20310.webp",
"my_id": 0,
"my_watched_episodes": 0,
"my_start_date": "0000-00-00",
"my_finish_date": "0000-00-00",
"my_score": 0,
"my_status": 1,
"my_rewatching": 0,
"my_rewatching_ep": 0,
"my_last_updated": 1496668441,
"my_tags": ""
}, ETCETERA 1000 more like this one
I don't really care about the "mylist" or "myinfo" part, just the "anime" part is needed. There are about 1000 items.
I've validated my JSON and it is valid.
This is my code:
JSONObject object = new JSONObject(replacedString);
JSONArray replacedResponse = new JSONArray(replacedString);
and here is where my issue begins. I've also tried this:
JSONObject object = new JSONObject(replacedString);
JSONArray replacedResponse = object.getJSONArray("mylist");
and JSONObject object = new JSONObject(replacedString); JSONArray replacedResponse = object.getJSONArray("anime");
with similar results
What i'm I not seeing here? thanks in advance!
1 Answer 1
Please follow this code.
String stringObj = "[YOUR JSON]";
// first Convert string into JsonObject
try {
JSONObject jsonObject = new JSONObject(stringObj);
// Inside the above object you have "mylist" key and the respective JsonObject so
JSONObject myListObject = jsonObject.optJSONObject("mylist");
// Insdide mylist you have myinfo Json and anim JsonArray
if(myListObject == null) {
return;
}
JSONObject myinfoObject = myListObject.optJSONObject("myinfo");
JSONArray animeJsonArray = myListObject.optJSONArray("anime");
// check null for myinfoObject and animeJsonArray and do the operation
} catch (JSONException e) {
e.printStackTrace();
}
object -> getJsonObject(mylist) -> getJsonArray(anime)