2017-06-05 22 views
0

私はJSONArray要素にJSON文字列を解析しようとしているが、私がしようとしたとき、私は「JSONArrayに変換することができません」を取得解析JSON文字列(アンドロイド)

私の文字列は、この方法(しかし途中です長い):

{ 
"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 

私は本当に「マイリスト」または「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

マイリストは、私が試した – Fusselchen

+0

配列、MyInfoの(オブジェクト)アニメ(配列)とそのオブジェクト、...右のPOJOを取得するためにhttp://www.jsonschema2pojo.org/を試していませんこれもまた: JSONArray replacedResponse = object.getJSONArray( "anime"); それはthrows:org.json.JSONException:アニメ – emboole

+0

の値は、 'object - > getJsonObject(mylist) - > getJsonArray(anime)'のような結果にならないよりも値がありません。 – Fusselchen

答えて

3

このコードに従ってください。

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