2016-03-26 12 views
0

私は、次のJSONを、持っている次のようには複雑jsonarraysとJSONオブジェクトを含むJSONを解析できません

for (int i = 0; i < response.length(); i++) { 
        try { 
//      Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_SHORT).show(); 
         JSONObject value = response.getJSONObject(i); 
         JSONObject statutory_mappings = value.getJSONObject("statutory_mappings"); 
         Iterator<String> keys = statutory_mappings.keys(); 
         while (keys.hasNext()) { 
          String key = (String) keys.next(); 
          JSONObject statMap = statutory_mappings.getJSONObject(key); 

          //iterate geography id's in case if there comes any necessity 
          JSONArray geography_ids = statMap.getJSONArray("geography_ids"); 
          JSONObject approval_status = statMap.getJSONObject("approval_status"); 
          String approval_status_text = statMap.getString("approval_status_text"); 
          JSONArray compliance_names = statMap.getJSONArray("compliance_names"); 
          String statutory_nature_name = statMap.getString("statutory_nature_name"); 
          String country_id = statMap.getString("country_id"); 
          String domain_name = statMap.getString("domain_name"); 
          String industry_names = statMap.getString("industry_names"); 
          String is_active = statMap.getString("is_active"); 
          String statutory_nature_id = statMap.getString("statutory_nature_id"); 
          JSONArray statutory_ids = statMap.getJSONArray("statutory_ids"); 
          String country_name = statMap.getString("country_name"); 
          JSONArray industry_ids = statMap.getJSONArray("industry_ids"); 
          JSONArray geography_mappings = statMap.getJSONArray("geography_mappings"); 
          JSONArray compliances = statMap.getJSONArray("compliances"); 
          String compliance_id = statMap.getString("compliance_id"); 
          JSONArray inner_statutory_mappings = statMap.getJSONArray("statutory_mappings"); 
          String domain_id = statMap.getString("domain_id"); 

          String logs = geography_ids + ""+ approval_status + " "+approval_status_text + " "+ compliance_names + " "+ statutory_nature_name + " "+country_id + " "+domain_name + " "+ industry_names + " "+is_active + " "+statutory_ids + " "+statutory_nature_id + " "+ country_name + " "+industry_ids + " "+ geography_mappings + " "+compliances + " "+compliance_id + " "+inner_statutory_mappings + " "+domain_id; 

          Toast.makeText(getActivity(),geography_ids.toString(),Toast.LENGTH_SHORT).show(); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 

問題はイムができないということです、それを解析しようとしている

[ 
    "GetStatutoryMappingsSuccess", 
    { 
    "statutory_mappings": { 
     "5": { 
     "geography_ids": [ 
      2, 
      1 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": null, 
      "compliance_name": "Every Month Task" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 1, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory, Office", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      2 
     ], 
     "country_name": "iNDIA", 
     "industry_ids": [ 
      1, 
      2 
     ], 
     "geography_mappings": [ 
      "iNDIA >> North Region", 
      "iNDIA >> South Region" 
     ], 
     "compliances": [ 
      { 
      "description": "sDSFDSf", 
      "is_active": true, 
      "repeats_type_id": 2, 
      "statutory_provision": "Provision", 
      "compliance_task": "Every Month Task", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 2, 
      "repeats_every": 1, 
      "statutory_dates": [ 
       { 
       "statutory_month": 1, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 2, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 3, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 4, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 5, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 6, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 7, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 8, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 9, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 10, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 11, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       }, 
       { 
       "statutory_month": 12, 
       "statutory_date": 10, 
       "trigger_before_days": 10, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 12 
      } 
     ], 
     "statutory_mappings": [ 
      "POG Act>>State Rule All" 
     ], 
     "domain_id": 2 
     }, 
     "6": { 
     "geography_ids": [ 
      17, 
      16 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": null, 
      "compliance_name": "One Time Task" 
      }, 
      { 
      "url": null, 
      "compliance_name": "Daily Task" 
      }, 
      { 
      "url": null, 
      "compliance_name": "Monthly Task" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 3, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory, Office", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      3 
     ], 
     "country_name": "Singapore", 
     "industry_ids": [ 
      1, 
      2 
     ], 
     "geography_mappings": [ 
      "Singapore >> North Region", 
      "Singapore >> South Region" 
     ], 
     "compliances": [ 
      { 
      "description": "FDSFDS", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Central Rule", 
      "compliance_task": "One Time Task", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": 1, 
       "statutory_date": 10, 
       "trigger_before_days": 25, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 13 
      }, 
      { 
      "description": "SDFDSF", 
      "is_active": true, 
      "repeats_type_id": 1, 
      "statutory_provision": "Central Rule", 
      "compliance_task": "Daily Task", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 2, 
      "repeats_every": 2, 
      "statutory_dates": [ 
       { 
       "statutory_month": null, 
       "statutory_date": null, 
       "trigger_before_days": 25, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 14 
      }, 
      { 
      "description": "DSDSD", 
      "is_active": true, 
      "repeats_type_id": 2, 
      "statutory_provision": "Central Rule", 
      "compliance_task": "Monthly Task", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 2, 
      "repeats_every": 1, 
      "statutory_dates": [ 
       { 
       "statutory_month": null, 
       "statutory_date": 3, 
       "trigger_before_days": 30, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 15 
      } 
     ], 
     "statutory_mappings": [ 
      "SOG Act" 
     ], 
     "domain_id": 2 
     }, 
     "7": { 
     "geography_ids": [ 
      2 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": null, 
      "compliance_name": "… … … ……… …dfg dfg ⁜ ⁜ ⁜ sdfsdsf sf sdfsd sdf sff⁙ ⁙ ⁙ ⁙ ⁙" 
      }, 
      { 
      "url": null, 
      "compliance_name": "⁕⁖ ⁗⁘ ⁙⁚ ⁛ ⁜gdfg ⁝ ⁞ ⁤⁥ ⁰⁵ ⁿ ⁑ ⁒ ⁓ ⁋ df sdf sdfdgf dfg dg" 
      }, 
      { 
      "url": null, 
      "compliance_name": "```````````````" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 1, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      2 
     ], 
     "country_name": "iNDIA", 
     "industry_ids": [ 
      1 
     ], 
     "geography_mappings": [ 
      "iNDIA >> North Region" 
     ], 
     "compliances": [ 
      { 
      "description": "Application for registration and grant of renewal of licen dfgce for the year … …… …… ⁜ ⁜", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "sadsad", 
      "compliance_task": "… … … ……… …dfg dfg ⁜ ⁜ ⁜ sdfsdsf sf sdfsd sdf sff⁙ ⁙ ⁙ ⁙ ⁙", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": 1, 
       "statutory_date": 4, 
       "trigger_before_days": null, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 16 
      }, 
      { 
      "description": "sdfsdfdsf", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "dssfdsf", 
      "compliance_task": "⁕⁖ ⁗⁘ ⁙⁚ ⁛ ⁜gdfg ⁝ ⁞ ⁤⁥ ⁰⁵ ⁿ ⁑ ⁒ ⁓ ⁋ df sdf sdfdgf dfg dg", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": null, 
       "statutory_date": null, 
       "trigger_before_days": null, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 17 
      }, 
      { 
      "description": "fghfg", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "gfghfg", 
      "compliance_task": "```````````````", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": null, 
       "statutory_date": null, 
       "trigger_before_days": null, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 18 
      } 
     ], 
     "statutory_mappings": [ 
      "POG Act>>State Rule All" 
     ], 
     "domain_id": 2 
     }, 
     "10": { 
     "geography_ids": [ 
      1 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": null, 
      "compliance_name": "One Time Task" 
      }, 
      { 
      "url": null, 
      "compliance_name": "One Time Tasss replicate" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 1, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      7 
     ], 
     "country_name": "iNDIA", 
     "industry_ids": [ 
      1 
     ], 
     "geography_mappings": [ 
      "iNDIA >> South Region" 
     ], 
     "compliances": [ 
      { 
      "description": "DSFSDFDS", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Central Rule", 
      "compliance_task": "One Time Task", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": 4, 
       "statutory_date": 12, 
       "trigger_before_days": 25, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 21 
      }, 
      { 
      "description": "sdfds", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Central Rule", 
      "compliance_task": "One Time Tasss replicate", 
      "format_file_list": null, 
      "duration": null, 
      "document_name": null, 
      "penal_consequences": "", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": 5, 
       "statutory_date": 4, 
       "trigger_before_days": 30, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 22 
      } 
     ], 
     "statutory_mappings": [ 
      "ESI Act>>Rule 1A" 
     ], 
     "domain_id": 2 
     }, 
     "11": { 
     "geography_ids": [ 
      1 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": "compliance_format/statutory-mapping-table-reference-c4f43cb06e24410fbed1f4d96a3afff8.xls", 
      "compliance_name": "Docu - One Time Task" 
      }, 
      { 
      "url": "compliance_format/md5-decrypt-508ffc6b93de45828b38abc9ce21ee18.py", 
      "compliance_name": "DOC - On Occurance Task" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 1, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      7 
     ], 
     "country_name": "iNDIA", 
     "industry_ids": [ 
      1 
     ], 
     "geography_mappings": [ 
      "iNDIA >> South Region" 
     ], 
     "compliances": [ 
      { 
      "description": "DSDSF", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Doc", 
      "compliance_task": "One Time Task", 
      "format_file_list": [ 
       { 
       "file_content": "compliance_format/statutory-mapping-table-reference-c4f43cb06e24410fbed1f4d96a3afff8.xls", 
       "file_name": "statutory-mapping-table-reference-c4f43cb06e24410fbed1f4d96a3afff8.xls", 
       "file_size": 11264 
       } 
      ], 
      "duration": null, 
      "document_name": "Docu", 
      "penal_consequences": "sdfdsf", 
      "duration_type_id": null, 
      "frequency_id": 1, 
      "repeats_every": null, 
      "statutory_dates": [ 
       { 
       "statutory_month": null, 
       "statutory_date": null, 
       "trigger_before_days": null, 
       "repeat_by": "dayofmonth" 
       } 
      ], 
      "compliance_id": 25 
      }, 
      { 
      "description": "sddsfdsf", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Doc", 
      "compliance_task": "On Occurance Task", 
      "format_file_list": [ 
       { 
       "file_content": "compliance_format/md5-decrypt-508ffc6b93de45828b38abc9ce21ee18.py", 
       "file_name": "md5-decrypt-508ffc6b93de45828b38abc9ce21ee18.py", 
       "file_size": 739 
       } 
      ], 
      "duration": 7, 
      "document_name": "DOC", 
      "penal_consequences": "", 
      "duration_type_id": 2, 
      "frequency_id": 4, 
      "repeats_every": null, 
      "statutory_dates": [], 
      "compliance_id": 23 
      } 
     ], 
     "statutory_mappings": [ 
      "ESI Act>>Rule 1A" 
     ], 
     "domain_id": 2 
     }, 
     "12": { 
     "geography_ids": [ 
      1 
     ], 
     "approval_status": 0, 
     "approval_status_text": "Pending", 
     "compliance_names": [ 
      { 
      "url": "compliance_format/mobile-api-50d3dfd554664da1bc65935202bf63b6.txt", 
      "compliance_name": "Doc - Doc" 
      } 
     ], 
     "statutory_nature_name": "Central", 
     "country_id": 1, 
     "domain_name": "Labour Law", 
     "industry_names": "Factory", 
     "is_active": true, 
     "statutory_nature_id": 2, 
     "statutory_ids": [ 
      7 
     ], 
     "country_name": "iNDIA", 
     "industry_ids": [ 
      1 
     ], 
     "geography_mappings": [ 
      "iNDIA >> South Region" 
     ], 
     "compliances": [ 
      { 
      "description": "Doc", 
      "is_active": true, 
      "repeats_type_id": null, 
      "statutory_provision": "Rule IA", 
      "compliance_task": "Doc", 
      "format_file_list": [ 
       { 
       "file_content": "compliance_format/mobile-api-50d3dfd554664da1bc65935202bf63b6.txt", 
       "file_name": "mobile-api-50d3dfd554664da1bc65935202bf63b6.txt", 
       "file_size": 6733 
       } 
      ], 
      "duration": 1, 
      "document_name": "Doc", 
      "penal_consequences": "sdfd", 
      "duration_type_id": 1, 
      "frequency_id": 4, 
      "repeats_every": null, 
      "statutory_dates": [], 
      "compliance_id": 24 
      } 
     ], 
     "statutory_mappings": [ 
      "ESI Act>>Rule 1A" 
     ], 
     "domain_id": 2 
     } 
    } 
    } 
] 

イムそれを解析することができます。 geography_idsは印刷されますが、承認ステータスやその他の値は印刷されません。どうしてこんなことに?私は何かの論理を欠いていますか?

+1

jsonに名前と値のペアがあり、geography_idsに名前がないため、jsonが適切でないと思います。 http://www.tutorialspoint.com/json/json_syntax.htm –

+2

ちょうど親切なアドバイス..このような複雑なjsonを手動で扱う代わりに、Gsonを使うことができます。 https://guides.codepath.com/android/Leveraging-the-Gson-Library – Ozgur

答えて

1

問題1あなたのJSONは、この形式は

:あなたはこのアレイの上itterateしようとするので、あなたが得る

[ 
    "GetStatutoryMappingsSuccess", 
    {...} 
] 

を1 Exeption:1番目の要素ではない org.json.JSONException: JSONArray[0] is not a JSONObject.ので、 JsonObjectではなくStringです。あなたがより良いだけではなく、ループ


問題2

のための JSONObject value = new JSONArray(json).getJSONObject(1);を使用したい理由は、

インサイド2番目のオブジェクトは、あなたが

{ 
    "statutory_mappings": { 
     "5": { 
      "approval_status": 0, 
      ... 
     } 
    } 
} 

を持っており、あなたの解析コードは動作するはずです"approval_status"はの整数なので、

の代わりに
JSONObject approval_status = statMap.getJSONObject("approval_status"); 

は次のようになります。

int approval_status = statMap.getInt("approval_status"); 

// Same problem for: 

int country_id = statMap.getInt("country_id"); 
boolean is_active = statMap.getBoolean("is_active"); 
int statutory_nature_id = statMap.getInt("statutory_nature_id"); 
int domain_id = statMap.getInt("domain_id"); 

問題3

compliance_idに問題があるとJSONがこの

"compliances": [ 
    { 
     // ... some fields here 
     "compliance_id": 13 
    }, 
    { 
     // ... some fields here 
     "compliance_id": 14 
    }, 
    ... 
] 

のように見えるので、印刷にも意味がありませんあなたのログ文字列にcompliancesとあなたが本当に配列内のAACH要素のcompliance_idを取得したいあなたは、この

JSONArray compliancesArr = statMap.getJSONArray("compliances"); 
for(int i = 0; i < compliancesArr.length();i++){ 
    JSONObject compliancesObj = compliancesArr.getJSONObject(i); 
    int compliance_id = compliancesObj.getInt("compliance_id"); 
} 

PSのようにそれらの上にitterateする必要があります。 GsonまたはJacksonは、解析コードをもっと簡単にします。

関連する問題