-2

Here is the JSON string that needs to be converted:

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

I need the inner loop data as new table. ie the first table should contain 2 rows which should have "ctin" and "inv" in it. Similarly the second table should have the inum details and the third table should have the item details.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can we see what code you have so far? Remember that while people here are willing to help, Stack Overflow is not a free coding service. – halfer Nov 21 '16 at 13:00
  • Possible duplicate http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object – thebenman Nov 21 '16 at 13:01

2 Answers2

0

I suggest you follow the bellow steps:

  1. Copy your JSON string
  2. Go to Visual Studio and press this menu item: Edit > Paste Special > Paste JSON as classes
  3. Enjoy the result!

Appropriate classes:

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

Note: Also you can use some attributes like [DataContract(Name ="...")] over the classes and [DataMember(Name = "...")] over the properties to make some differents between C# and JSON property names.

You should replace ... with a name

Reference

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
0

With json2charp you can easily generate C# classes for parsing from the JSON. For your data you can use:

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

You can remove the properties you do not need. Parsing with JSON.NET is like

var data = JsonConvert.DeserializeObject<RootObject>(jsonString);
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67