我正在尝试将json字符串解析为JSONArray元素,但是当我尝试时,我得到“无法转换为JSONArray”
我的字符串是这种方式(但更长):
{
"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
我并不真正在乎“ mylist”或“ myinfo”部分,只需要“ anime”部分。大约有1000个项目。
我已经验证了JSON,它是有效的。
这是我的代码:
JSONObject object = new JSONObject(replacedString);
JSONArray replacedResponse = new JSONArray(replacedString);
这是我的问题开始的地方。我也尝试过这个:
JSONObject object = new JSONObject(replacedString);
JSONArray replacedResponse = object.getJSONArray("mylist");
和
JSONObject对象=新的JSONObject(replacedString);
JSONArray替换了Response = object.getJSONArray(“ anime”);
结果相似
我在这里没看到什么?提前致谢!
最佳答案
请遵循此代码。
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();
}