如何在c#中将复杂的JSON字符串反序列化为数据集?

时间:2016-11-21 12:38:43

标签: c# json dataset deserialization

以下是需要转换的JSON字符串:

{
    "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
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

我需要内部循环数据作为新表。即第一个表应包含2行,其中应包含“ctin”和“inv”。类似地,第二个表应该具有inum细节,第三个表应该具有项目细节。

2 个答案:

答案 0 :(得分:0)

我建议你遵循以下步骤:

  1. 复制JSON字符串
  2. 转到Visual Studio并按此菜单项:Edit > Paste Special > Paste JSON as classes
  3. 享受结果!
  4. 适当的课程:

    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#和{{1}之间进行一些区分属性名称。

         

    您应该使用名称

    替换JSON      

    Reference

答案 1 :(得分:0)

使用json2charp,您可以轻松生成C#类,以便从JSON进行解析。您可以使用以下数据:

@media only screen and (max-width:768px)
   {
   /*do something*/ 
   }

您可以删除不需要的属性。使用JSON.NET进行解析就像

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