Home > Net >  How json into a datatable?
How json into a datatable?

Time:09-15

Json format below

{
"HEAD" : {
"BillType" : "S - W,"
"BillNo" : "BSN0001
"},
"ITEM" : [
{
"ItemID" : "IT0002,"

"Qty" : "1500"
},
{
"ItemID" : "IT0003,"

"Qty" : "5300"
}
]
}

Need to convert the datatable below
BillType billNo ItemID Qty
S - W BSN0001 IT0002 1500
S - W BSN0001 IT0003 5300

CodePudding user response:

Read the json serialization into objects, and then to operate this object is ok,

CodePudding user response:

reference 1/f, nuts, o response:
the json deserialized into objects, to operate the object,

The example above, is a collection of objects?

CodePudding user response:

refer to the second floor weisai response:
Quote: refer to 1st floor nuts o response:
the json deserialized into objects, to operate the object,

The example above, is a collection of objects?
the json string is obviously a object, the object inside a attribute is set,

CodePudding user response:

Force can realize that you, you see,
 
String jsonStr="{" HEAD \ ": {" billType \ ": " S - W \ ", \ "billNo " : \} "BSN0001 " and \ "ITEM " : [{\ "ItemID " : \ "IT0002 ", \ "Qty " : \ "1500 "}, {\ "ItemID " : \ "IT0003 ", \ "Qty " : \ "5300 "}]} ";
The Test Test=JsonConvert. DeserializeObject (jsonStr);

The DataTable dt=new DataTable ();
Dt. Columns. The Add (new DataColumn (" BillType "));
Dt. Columns. The Add (new DataColumn (" BillNo "));
Dt. Columns. The Add (new DataColumn (" ItemID "));
Dt. Columns. The Add (new DataColumn (" Qty "));

Foreach (var item in the test item)
{
DataRow Dr=dt. NewRow ();
Dr [" BillType "]=test. Head. BillType;
Dr [" BillNo "]=test. Head. BillNo.
Dr (" ItemID ")=item. The ItemID.
Dr [" Qty "]=item. Qty.
Dt. Rows. The Add (Dr);
}

The class Test
{
Public Head Head {get; set; }
Public List Item {get; set; }
}

The class Head
{
Public string BillType {get; set; }
Public string BillNo {get; set; }
}

The class TestItem
{
Public string ItemID {get; set; }
Public string Qty {get; set; }
}
  •  Tags:  
  • C#
  • Related