如何在c#中将复杂的JSON字符串反序列化到数据集中?
以下是需要转换的JSON字符串:如何在c#中将复杂的JSON字符串反序列化到数据集中?,c#,json,dataset,deserialization,C#,Json,Dataset,Deserialization,以下是需要转换的JSON字符串: { "b2b": [ { "ctin": "37ABCDE9552F3Z4", "inv": [ { "inum": "S008400", "idt": "09-04-2016", "val": 861786.91,
{
"b2b": [
{
"ctin": "37ABCDE9552F3Z4",
"inv": [
{
"inum": "S008400",
"idt": "09-04-2016",
"val": 861786.91,
"pos": "6",
"rchrg": "No",
"pro_ass": "Y",
"itms": [
{
"num": 1,
"itm_det": {
"ty": "S",
"hsn_sc": "H724",
"txval": 5589.87,
"irt": 0.0,
"iamt": 0.0,
"crt": 87.92,
"camt": 5.7947562568E8,
"srt": 86.56,
"samt": 50.74
}
},
{
"num": 2,
"itm_det": {
"ty": "S",
"hsn_sc": "H863",
"txval": 2509.27,
"irt": 0.0,
"iamt": 0.0,
"crt": 12.99,
"camt": 26144.48,
"srt": 31.81,
"samt": 276654.5
}
}
]
}
]
},
{
"ctin": "76ABCDE2148F9Z9",
"inv": [
{
"chksum": "AflJufPlFStqKBZ",
"inum": "S008400",
"idt": "24-11-2016",
"val": 729248.16,
"pos": "6",
"rchrg": "No",
"pro_ass": "Y",
"itms": [
{
"num": 1,
"itm_det": {
"ty": "S",
"hsn_sc": "S8590",
"txval": 8196.88,
"irt": 0.0,
"iamt": 0.0,
"crt": 42.44,
"camt": 202.86,
"srt": 40.99,
"samt": 0.02
}
},
{
"num": 2,
"itm_det": {
"ty": "S",
"hsn_sc": "H357",
"txval": 6760.14,
"irt": 0.0,
"iamt": 0.0,
"crt": 23.89,
"camt": 6.8214986738E8,
"srt": 60.95,
"samt": 0.03
}
}
]
}
]
}
]
}
我需要内部循环数据作为新表。即第一个表应包含两行,其中应包含“ctin”和“inv”。同样,第二个表应包含inum详细信息,第三个表应包含项目详细信息。我建议您遵循以下步骤:
JSON
字符串Edit>Paste Special>Paste JSON as class
public class Rootobject
{
public B2b[] b2b { get; set; }
}
public class B2b
{
public string ctin { get; set; }
public Inv[] inv { get; set; }
}
public class Inv
{
public string inum { get; set; }
public string idt { get; set; }
public float val { get; set; }
public string pos { get; set; }
public string rchrg { get; set; }
public string pro_ass { get; set; }
public Itm[] itms { get; set; }
public string chksum { get; set; }
}
public class Itm
{
public int num { get; set; }
public Itm_Det itm_det { get; set; }
}
public class Itm_Det
{
public string ty { get; set; }
public string hsn_sc { get; set; }
public float txval { get; set; }
public float irt { get; set; }
public float iamt { get; set; }
public float crt { get; set; }
public float camt { get; set; }
public float srt { get; set; }
public float samt { get; set; }
}
注意:您还可以在类上使用一些属性,如[DataContract(Name=“…”)
,在属性上使用[DataMember(Name=“…””)
,以在C#
和JSON
属性名称之间进行一些区别
您应该用名称替换…
使用JSON,您可以轻松生成用于解析的C#类。对于您的数据,您可以使用:
public class ItmDet
{
public string ty { get; set; }
public string hsn_sc { get; set; }
public double txval { get; set; }
public double irt { get; set; }
public double iamt { get; set; }
public double crt { get; set; }
public double camt { get; set; }
public double srt { get; set; }
public double samt { get; set; }
}
public class Itm
{
public int num { get; set; }
public ItmDet itm_det { get; set; }
}
public class Inv
{
public string inum { get; set; }
public string idt { get; set; }
public double val { get; set; }
public string pos { get; set; }
public string rchrg { get; set; }
public string pro_ass { get; set; }
public List<Itm> itms { get; set; }
public string chksum { get; set; }
}
public class B2b
{
public string ctin { get; set; }
public List<Inv> inv { get; set; }
}
public class RootObject
{
public List<B2b> b2b { get; set; }
}
公共类ItmDet
{
公共字符串ty{get;set;}
公共字符串hsn_sc{get;set;}
公共双txval{get;set;}
公共双irt{get;set;}
公共双iamt{get;set;}
公共双crt{get;set;}
公共双camt{get;set;}
公共双srt{get;set;}
公共双samt{get;set;}
}
公共类Itm
{
公共int num{get;set;}
公共ItmDet itm_det{get;set;}
}
公共类投资
{
公共字符串inum{get;set;}
公共字符串idt{get;set;}
公共双val{get;set;}
公共字符串pos{get;set;}
公共字符串rchrg{get;set;}
公共字符串pro_ass{get;set;}
公共列表itms{get;set;}
公共字符串chksum{get;set;}
}
公共类B2b
{
公共字符串ctin{get;set;}
公共列表inv{get;set;}
}
公共类根对象
{
公共列表b2b{get;set;}
}
您可以删除不需要的属性。使用JSON.NET解析类似于
var data = JsonConvert.DeserializeObject<RootObject>(jsonString);
var data=JsonConvert.DeserializeObject(jsonString);
我们可以看看您目前有哪些代码吗?请记住,虽然这里的人愿意提供帮助,但堆栈溢出并不是免费的编码服务。可能重复