0

I have got a following 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
      }
    }
  }
]

Im trying to parse it as follows,

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

The problem is that Im not be able to parse it. It prints geography_ids but not approval status and other values. Why is that so? Am I missing any logic?

2

1 Answer 1

1

Problem 1

Your json has this format:

[
    "GetStatutoryMappingsSuccess",
    {...}
]

So when you try to itterate over this array you will get 1 Exeption: org.json.JSONException: JSONArray[0] is not a JSONObject. because 1-st element is not a JsonObject but a String. That is why you'd better just use JSONObject value = new JSONArray(json).getJSONObject(1); instead of for loop


Problem 2

Inside 2-nd object you have

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

And your parsing code should work, but "approval_status" is integer , so instead of

JSONObject approval_status = statMap.getJSONObject("approval_status");

should be:

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");

Problem 3

There is also problem with compliance_id and json looks like this

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

So there is no point to print compliances in your log string and if you really want to get compliance_id of aach element in array you have to itterate over them like this

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 or Jackson will make your parsing code much easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.