在c#中将复杂的json字符串转换为datatable

时间:2017-10-24 07:07:59

标签: c# .net json datatable dataset

我有一个JSON文件,其中包含我需要转换为DataSet的复杂数据。

{
"status_cd": "PE",
"fp": "072017",
"error_report": {
  "b2b": [
   {
    "inv": [
      {
        "val": 3623,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 138.1,
              "csamt": 0,
              "rt": 12,
              "txval": 2301.67,
              "camt": 138.1
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 34.65,
              "csamt": 0,
              "rt": 28,
              "txval": 247.51,
              "camt": 34.65
            }
          },
          {
            "num": 3,
            "itm_det": {
              "samt": 55.52,
              "csamt": 0,
              "rt": 18,
              "txval": 616.88,
              "camt": 55.52
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "11-07-2017",
        "rchrg": "N",
        "inum": "GCC006062"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ACUPS3531N2ZS",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 227,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 12.14,
              "csamt": 0,
              "rt": 12,
              "txval": 202.32,
              "camt": 12.14
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "12-07-2017",
        "rchrg": "N",
        "inum": "GCC006099"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01DGRPS9260H1ZK",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 689,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 36.9,
              "csamt": 0,
              "rt": 12,
              "txval": 615.01,
              "camt": 36.9
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "14-07-2017",
        "rchrg": "N",
        "inum": "GCC006166"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ARRPS5140D1ZX",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 250,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 13.39,
              "csamt": 0,
              "rt": 12,
              "txval": 223.22,
              "camt": 13.39
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "17-07-2017",
        "rchrg": "N",
        "inum": "GCC006285"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ARRPS5140D1ZX",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 1478,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 79.2,
              "csamt": 0,
              "rt": 12,
              "txval": 1320,
              "camt": 79.2
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "17-07-2017",
        "rchrg": "N",
        "inum": "GCC006293"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AFMPS1072H1ZC",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 1290,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 60.48,
              "csamt": 0,
              "rt": 12,
              "txval": 1008.01,
              "camt": 60.48
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 17.59,
              "csamt": 0,
              "rt": 28,
              "txval": 125.67,
              "camt": 17.59
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "17-07-2017",
        "rchrg": "N",
        "inum": "GCC006315"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AJCTS9401L1Z8",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 6774,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 67.64,
              "csamt": 0,
              "rt": 12,
              "txval": 1127.3,
              "camt": 67.64
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 420.4,
              "csamt": 0,
              "rt": 18,
              "txval": 4671.06,
              "camt": 420.4
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "21-07-2017",
        "rchrg": "N",
        "inum": "GCC006580"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01CPOPS8196Q1Z0",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 410,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 13.59,
              "csamt": 0,
              "rt": 12,
              "txval": 226.54,
              "camt": 13.59
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 11.89,
              "csamt": 0,
              "rt": 18,
              "txval": 132.15,
              "camt": 11.89
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "22-07-2017",
        "rchrg": "N",
        "inum": "GCC006608"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ASZPB4433B1Z3",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 749,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 57.13,
              "csamt": 0,
              "rt": 18,
              "txval": 634.83,
              "camt": 57.13
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "22-07-2017",
        "rchrg": "N",
        "inum": "GCC006612"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ARRPS5140D1ZX",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 1318,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 100.54,
              "csamt": 0,
              "rt": 18,
              "txval": 1117.1,
              "camt": 100.54
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "22-07-2017",
        "rchrg": "N",
        "inum": "GCC006673"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ANSPA6465C1Z3",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 197,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 10.54,
              "csamt": 0,
              "rt": 12,
              "txval": 175.61,
              "camt": 10.54
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "24-07-2017",
        "rchrg": "N",
        "inum": "GCC006726"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ARRPS5140D1ZX",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 380,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 20.37,
              "csamt": 0,
              "rt": 12,
              "txval": 339.55,
              "camt": 20.37
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "25-07-2017",
        "rchrg": "N",
        "inum": "GCC006772"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AACFI0598P126",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 2766,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 135.47,
              "csamt": 0,
              "rt": 12,
              "txval": 2257.8,
              "camt": 135.47
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 25.97,
              "csamt": 0,
              "rt": 28,
              "txval": 185.47,
              "camt": 25.97
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "25-07-2017",
        "rchrg": "N",
        "inum": "GCC006777"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ACUPS3531N2ZS",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 738,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 17.56,
              "csamt": 0,
              "rt": 5,
              "txval": 702.39,
              "camt": 17.56
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "26-07-2017",
        "rchrg": "N",
        "inum": "GCC006823"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AUXPS7983G2ZR",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 2439,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 62.88,
              "csamt": 0,
              "rt": 12,
              "txval": 1048,
              "camt": 62.88
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 96.52,
              "csamt": 0,
              "rt": 18,
              "txval": 1072.4,
              "camt": 96.52
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "26-07-2017",
        "rchrg": "N",
        "inum": "GCC006828"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AABPG0741N1ZK",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 2439,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 62.88,
              "csamt": 0,
              "rt": 12,
              "txval": 1048,
              "camt": 62.88
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 96.52,
              "csamt": 0,
              "rt": 18,
              "txval": 1072.4,
              "camt": 96.52
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "26-07-2017",
        "rchrg": "N",
        "inum": "GCC006830"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AABPG0741N1ZK",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 5979,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 313.09,
              "csamt": 0,
              "rt": 12,
              "txval": 5218.13,
              "camt": 313.09
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 10.27,
              "csamt": 0,
              "rt": 18,
              "txval": 114.09,
              "camt": 10.27
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "27-07-2017",
        "rchrg": "N",
        "inum": "GCC006871"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ABWPG7905P1ZY",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 4879,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 212.83,
              "csamt": 0,
              "rt": 12,
              "txval": 3547.23,
              "camt": 212.83
            }
          },
          {
            "num": 2,
            "itm_det": {
              "samt": 69.15,
              "csamt": 0,
              "rt": 18,
              "txval": 768.3,
              "camt": 69.15
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "28-07-2017",
        "rchrg": "N",
        "inum": "GCC006943"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01ACUPS3531N2ZS",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 2756,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 147.66,
              "csamt": 0,
              "rt": 12,
              "txval": 2460.97,
              "camt": 147.66
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "29-07-2017",
        "rchrg": "N",
        "inum": "GCC007016"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AJCTS9401L1Z8",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 2857,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 38.59,
              "csamt": 0,
              "rt": 12,
              "txval": 643.16,
              "camt": 38.59
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "31-07-2017",
        "rchrg": "N",
        "inum": "GCC007047"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AUXPS7983G2ZR",
    "error_cd": "RET191113"
  },
  {
    "inv": [
      {
        "val": 1296,
        "itms": [
          {
            "num": 1,
            "itm_det": {
              "samt": 69.45,
              "csamt": 0,
              "rt": 12,
              "txval": 1157.57,
              "camt": 69.45
            }
          }
        ],
        "inv_typ": "R",
        "pos": "01",
        "idt": "31-07-2017",
        "rchrg": "N",
        "inum": "GCC007110"
      }
    ],
    "error_msg": "The GSTIN is invalid. Please enter a valid GSTIN.",
    "ctin": "01AASPJ1987C1ZX",
    "error_cd": "RET191113"
  },      
]
},
"gstin": "01AAYPG1879R1ZV"
}

我已经尝试将其反序列化为通用数据表,但它不能正常工作。 使用类结构它不适合。任何人都可以告诉我如何解决它。

错误是:

  

Newtonsoft.Json.JsonSerializationException:'完成反序列化对象后在JSON字符串中找到的附加文本'

1 个答案:

答案 0 :(得分:1)

您的JSON不适合直接转换为DataSet。相反,您可以反序列化为一个类,然后从那里采取步骤。

班级:

public class ItmDet
{
    public double samt { get; set; }
    public int csamt { get; set; }
    public int rt { get; set; }
    public double txval { get; set; }
    public double camt { get; set; }
}

public class Itm
{
    public int num { get; set; }
    public ItmDet itm_det { get; set; }
}

public class Inv
{
    public int val { get; set; }
    public List<Itm> itms { get; set; }
    public string inv_typ { get; set; }
    public string pos { get; set; }
    public string idt { get; set; }
    public string rchrg { get; set; }
    public string inum { get; set; }
}

public class B2b
{
    public List<Inv> inv { get; set; }
    public string error_msg { get; set; }
    public string ctin { get; set; }
    public string error_cd { get; set; }
}

public class ErrorReport
{
    public List<B2b> b2b { get; set; }
}

public class RootObject
{
    public string status_cd { get; set; }
    public string fp { get; set; }
    public ErrorReport error_report { get; set; }
    public string gstin { get; set; }
}

然后你只需反序列化它:

var data = JsonConvert.DeserializeObject<RootObject>(json);

仅供参考,这是JSON可直接反序列化为DataSet的内容:

{
    "Table1": [
    {
        "Value1": "ABC",
        "Value2": "123"
    }
    ],
    "Table2": [
    {
        "Value1": "DEF",
        "Value2": "456"
    }
    ]
}