解析JSON字符串JSONArray(安卓)

问题描述:

我试图JSON字符串解析到一个JSONArray元素,但是当我尝试,我得到“无法转换到JSONArray”解析JSON字符串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”部分,只需要“动漫”部分。有大约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 replacedResponse = object.getJSONArray(“anime”);

类似的结果

我在这里没有看到什么?提前致谢!

+1

MYLIST不是一个数组,它与MyInfo的(对象)和动画(阵列)的对象,...尝试http://www.jsonschema2pojo.org/得到正确的POJO – Fusselchen

+0

我试过也与此: JSONArray replacedResponse = object.getJSONArray(“anime”); 它抛出:org.json.JSONException:没有值的动画 – emboole

+0

比它应该导致类似于'对象 - > getJsonObject(mylist) - > getJsonArray(动漫)' – Fusselchen

请按照此代码。

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(); 
    } 
+0

我是一个简单的人。我理解这个解释。我复制,粘贴和修改我自己的代码。有用。我将答案标记为正确。 – emboole