JSON到GridView解析| System.NullReferenceException

时间:2019-02-15 11:51:52

标签: c# json gridview

我正在通过将多个JSON文件读取为字符串,将它们解析为XMLNodes,并将XMNodeLists转换为Gridviews,来构建JSON到XLSX解析器。成功解析2-3个文件后,我不断收到NULLException错误。

我正在使用以下代码以及JSON的结构。

JSON:

{
  "id": "9903f87c-2ddc-11e9-8b28-42010a920002",
  "name": "AU_BLS Beneficiary Details",
  "key": "auBlsBeneficiaryDetails",
  "description": "",
  "editorJson": {
    "name": "AU_BLS Beneficiary Details",
    "key": "auBlsBeneficiaryDetails",
    "version": 0,
    "fields": [
      {
        "fieldType": "OptionFormField",
        "id": "modeOfDisbursal",
        "name": "Select Your Preferred Mode of Disbursal",
        "type": "dropdown",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "type": "string",
            "form_type": "dropdown",
            "default": "",
            "order": 2,
            "required": true,
            "disabled": false,
            "placeholder": false,
            "api": "",
            "colWidth": 4,
            "validator": "[]",
            "newRow": true,
            "display-on-selection": {
              "cheque": [ "uploadCancelledCheque1" ],
              "nach": [ "ifscCode1", "nameOfTheBank1", "branch1", "bankAccountNumber1" ]
            }
          }
        },
        "layout": null,
        "optionType": null,
        "hasEmptyValue": true,
        "options": [
          {
            "id": "cheque",
            "name": "Cheque"
          },
          {
            "id": "nach",
            "name": "NACH/ECS"
          }
        ],
        "optionsExpression": null
      },
      {
        "fieldType": "FormField",
        "id": "uploadCancelledCheque1",
        "name": "Upload Cancelled Cheque",
        "type": "upload",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "order": 4,
            "required": true,
            "disabled": false,
            "form_type": "upload",
            "validator": "[]",
            "dependsOn": [],
            "api": {
              "url": "http://35.200.188.121:8000/api/dms/kdms/documents/",
              "method": "POST",
              "data": { "document_type": 1 },
              "mapping": {},
              "depends-on": "[]"
            },
            "newRow": true,
            "colWidth": 8
          }
        },
        "layout": null
      },
      {
        "fieldType": "FormField",
        "id": "bankAccountNumber1",
        "name": "Bank Account Number",
        "type": "integer",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "type": "number",
            "form_type": "number",
            "default": "",
            "order": 5,
            "required": true,
            "disabled": false,
            "placeholder": false,
            "api": "",
            "newRow": true,
            "validator": "[]"
          }
        },
        "layout": null
      },
      {
        "fieldType": "FormField",
        "id": "ifscCode1",
        "name": "IFSC Code",
        "type": "integer",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "type": "rest",
            "form_type": "text",
            "default": "",
            "order": 6,
            "required": true,
            "disabled": false,
            "placeholder": false,
            "api": {
              "url": "http://kuliza.mockable.io/ifsc",
              "method": "POST",
              "data": { "ifsc": "ifscCode1" },
              "mapping": {
                "nameOfTheBank1": "data.BankName",
                "branch1": "data.BranchName"
              }
            },
            "validator": "[]"
          }
        },
        "layout": null
      },
      {
        "fieldType": "FormField",
        "id": "nameOfTheBank1",
        "name": "Name of the Bank",
        "type": "text",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "type": "string",
            "form_type": "text",
            "default": "",
            "order": 7,
            "required": true,
            "disabled": false,
            "placeholder": false,
            "api": "",
            "validator": "[]",
            "newRow": true,
            "colWidth": 4
          }
        },
        "layout": null
      },
      {
        "fieldType": "FormField",
        "id": "branch1",
        "name": "Branch",
        "type": "text",
        "value": null,
        "required": false,
        "readOnly": false,
        "overrideId": true,
        "placeholder": null,
        "params": {
          "meta": {
            "type": "string",
            "form_type": "text",
            "default": "",
            "order": 8,
            "required": true,
            "disabled": false,
            "placeholder": false,
            "api": "",
            "validator": "[]",
            "colWidth": 4
          }
        },
        "layout": null
      }
    ],
    "outcomes": []
  }
}
var formsPath = "/Uploads";
            var finalPath = string.Concat(System.IO.Path.Combine(Server.MapPath("~/Uploads")), formsPath);
            string[] filePaths = Directory.GetFiles(finalPath, "*.json");

            var ds = new DataSet();

            for (int i = 0; i < filePaths.Length; i++)
            {
                var json = File.ReadAllText(filePaths[i]);

                var xmlDoc = JsonConvert.DeserializeXmlNode(json, "editorJson");
                var xmlNode = xmlDoc.GetElementsByTagName("fields");

                if (xmlNode.Count > 0)
                {
                    DataTable dt = ConvertXmlNodeListToDataTable(xmlNode);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        GridView1.DataSource = dt;
                    }
                }
                GridView1.DataBind();
                string pathOfFile = Path.GetFileNameWithoutExtension(filePaths[i]);
                ds.Tables.Add(MakeDtReady(pathOfFile));
                Console.WriteLine("File {0} done", i);
                GridView1.DataSource = null;
                GridView1.DataBind();

            }

            using (var wb = new XLWorkbook())
            {
                foreach (DataTable dt in ds.Tables)
                {
                    wb.Worksheets.Add(dt);
                }

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=ConvertedFile.xlsx");
                using (var myMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(myMemoryStream);
                    myMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }



public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
        {
            return DataTable(xnl);
        }



private static DataTable DataTable(XmlNodeList xnl)
        {
            return Dt(xnl);
        }

        private static DataTable Dt(XmlNodeList xnl)
        {
            var dt = new DataTable();
            var tempColumn = 0;

            foreach (XmlNode node in xnl.Item(0).ChildNodes)
            {
                tempColumn++;
                var dc = new DataColumn(node.Name, Type.GetType("System.String"));
                if (dt.Columns.Contains(node.Name))
                {
                    dt.Columns.Add(dc.ColumnName = dc.ColumnName + tempColumn.ToString());
                }
                else
                {
                    dt.Columns.Add(dc);
                }
            }

            var columnsCount = dt.Columns.Count;
            Console.WriteLine("{0} done", columnsCount);
            for (var i = 0; i < xnl.Count; i++)
            {
                var dr = dt.NewRow();
                for (int j = 0; j < columnsCount; j++)
                {
                    dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
                }

                dt.Rows.Add(dr);
            }

            return dt;
        }
    }

1 个答案:

答案 0 :(得分:0)

检查集合xnl是否有任何带有断点的项目或if语句