簡體   English   中英

無法解析包含復雜jsonarrays和Json對象的JSON

[英]Unable to parse a JSON containing complex jsonarrays and Json objects

我有以下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_id,但不顯示批准狀態和其他值。 為什么會這樣? 我是否缺少任何邏輯?

問題1

您的json具有以下格式:

[
    "GetStatutoryMappingsSuccess",
    {...}
]

因此,當您嘗試對該數組進行仿真時,您將獲得1個Exeption: org.json.JSONException: JSONArray[0] is not a JSONObject. 因為1st元素不是JsonObject而是String。 這就是為什么最好只使用JSONObject value = new JSONArray(json).getJSONObject(1); 代替for循環


問題2

在第二個對象里面

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

並且您的解析代碼應該可以運行,但是“ approval_status”是integer ,所以不是

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 ,如果您真的想獲取數組中每個元素的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。 Gson或Jackson將使您的解析代碼更加容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM