[英]Parse JSON String into JSONArray (Android)
我正在嘗試將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 object = new 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.