AndroidStudio2.3.1でアプリを、作っています。
マストドンのJSONデータがあって取得対象の"reblog"項目は、存在する場合と存在しない場合があります。
下記のコードでは、頻繁にJSONExceptionが発生しますし有無確認のためにtry,catchの仕組みを、使うことに違和感があります。
JSONデータに項目が、存在するかチェックする方法で、より良いベストな方法が、あるでしょうか?
■コード
JSONArray jsonAry = new JSONArray(strMastodonJSON); for (int i = 0; i < jsonAry.length(); i++) { jsonObj = (JSONObject) jsonArray.get(i); flag = true; try { jsonObjReblog = (JSONObject)jsonObj.getJSONObject("reblog"); } catch (JSONException e) { e.printStackTrace(); flag = false; } 中略(ここにflagで条件分岐する表示データの配列操作処理がある)。。。 }
■JSONException
org.json.JSONException: Value null at reblog of type org.json.JSONObject$1 cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:100) at org.json.JSONObject.getJSONObject(JSONObject.java:613) 以下略。。。
■JSONデータ
{ "id": 12345678, "created_at": "2017-01-01T00:57:57.465Z", "in_reply_to_id": null, "in_reply_to_account_id": null, "sensitive": false, "spoiler_text": "", "visibility": "public", "application": null, 中略。。。 "url": "https://mstdn.jp/@null/12345678", "reblogs_count": 0, "favourites_count": 0, "reblog": { "id": 1234567, "created_at": "2017-01-01T00:00:00.000Z", "in_reply_to_id": null, "in_reply_to_account_id": null, "sensitive": false, "spoiler_text": "", "visibility": "public", "application": null, 中略。。。 "favourited": null, "reblogged": null }, "favourited": null, "reblogged": null }
IDやURLなどデータはダミーです。
宜しくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/04 04:59 編集