无法解析包含复杂jsonarrays和Json对象的JSON

时间:2016-03-26 08:26:33

标签: android json

我有一个跟随json,

[
  "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
      }
    }
  }
]

我试图按如下方式解析它,

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

问题在于我无法解析它。它打印geography_ids但不打印批准状态和其他值。为什么会这样?我错过了任何逻辑吗?

1 个答案:

答案 0 :(得分:1)

问题1

你的json有这种格式:

[
    "GetStatutoryMappingsSuccess",
    {...}
]

因此,当您尝试对此数组进行迭代时,您将获得1个Exeption: org.json.JSONException: JSONArray[0] is not a JSONObject.因为1-st元素不是JsonObject而是String。这就是为什么你最好只使用JSONObject value = new JSONArray(json).getJSONObject(1);代替for loop

问题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,你必须像这样对它们进行ittorate

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。 Gson或Jackson将使您的解析代码更容易。