显示RootObject类并将其显示在datatable中

时间:2016-12-30 08:42:24

标签: c# json datatable

我是初学者,我需要帮助显示包含2个对象列表的rootobject类,并将它们显示在数据表中。 我一直在寻找方法,但没有一个能帮助我。

如何访问RootObject类中的2个列表并将它们显示在数据表中?

以下是我的JSON文件

setMaxHeight()

下面是我将json数据反序列化为rootobject的代码

{
"Non_Portable": [
  {
    "NameOfGamingEquipments": "Playstation 4",
    "ResourceId": 1,
    "RentalPrice": 200,
    "DeliveryMode": "Hand Carry",
    "quantityOfCables": 2,
    "TypeOfCable": "Micro USB for controller and HDMI for display",
    "Accessories": "2 wireless Dualshock 4 controllers"
  },
  {
    "NameOfGamingEquipments": "Xbox One",
    "ResourceId": 2,
    "RentalPrice": 200,
    "DeliveryMode": " Hand Carry",
    "quantityOfCables": 2,
    "TypeOfCable": " Micro USB cable for controller and HDMI cable for display",
    "Accessories": "batteries for controller"
  },
  {
    "NameOfGamingEquipments": "Playstation 3",
    "ResourceId": 3,
    "RentalPrice": 120,
    "DeliveryMode": "delivery via deliveryman",
    "quantityOfCables": 1,
    "TypeOfCable": "HDMI cable for display",
    "Accessories": "Wireless dualshock 3 controller for Playstation 3"
  }

],


"Portable": [
  {
    "NameOfGamingEquipments": "Nintendo 3DS",
    "ResourceId": 4,
    "RentalPrice": 50,
    "DeliveryMode": "Hand carry",
    "sizeOfScreen": "Top: 4.88 Bottom: 4.18",
    "quantityOfCartridges": 1,
    "CartridgeName": "Super Mario",
    "touchScreenFunction": true

  },
  {
    "NameOfGamingEquipments": "Sony Playstation Vita",
    "ResourceId": 5,
    "RentalPrice": 70,
    "DeliveryMode": "Self Pick Up",
    "sizeOfScreen": "5 inches",
    "quantityOfCartridges": 2,
    "CartridgeName": "Powerpuff Girls and GTA ",
    "touchScreenFunction": true
  },
  {
    "NameOfGamingEquipments": "Nintendo 3DS XL",
    "ResourceId": 6,
    "RentalPrice": 40,
    "DeliveryMode": "Self Pick Up",
    "sizeOfScreen": "Top: 4.88 bottom: 4.18 ",
    "quantityOfCartridges": 1,
    "CartridgeName": "Ridge Racer",
    "touchScreenFunction": true
  }
]
}

提前感谢大家的帮助和建议!!!

更新:由Madhi帮助

    Rootobject ser;
    string jsonstr;
    private void Booking_Load(object sender, EventArgs e)
    {
      jsonstr = File.ReadAllText("Data.JSON");
       ser = JsonConvert.DeserializeObject<Rootobject>(jsonstr);

    }

我不确定桌子为什么没有出现..

1 个答案:

答案 0 :(得分:0)

我假设您的反序列化成功。然后你可以像这样填充数据表:

< img class="logo" src="/**resources**/images/blackcat.jpg"

对您的其他列表(便携式)执行相同操作。 这是结果:

enter image description here

RootObject类:

var json = File.ReadAllText(@"C:\Path\json.txt");
var test = JsonConvert.DeserializeObject<RootObject>(json);
DataTable dt = new DataTable();

dt.Columns.Add("NameOfGamingEquipment");
dt.Columns.Add("ResourceId");
dt.Columns.Add("RentalPrice");
dt.Columns.Add("DeliveryMode");
dt.Columns.Add("QuantityOfCables");
dt.Columns.Add("TypeOfCable");
dt.Columns.Add("Accessories");
foreach (var item in test.Non_Portable)
{
    var row = dt.NewRow();
    row["NameOfGamingEquipment"] = item.NameOfGamingEquipments;
    row["ResourceId"] = Convert.ToString(item.ResourceId);
    row["RentalPrice"] = item.RentalPrice;
    row["DeliveryMode"] = item.DeliveryMode;
    row["quantityOfCables"] = item.quantityOfCables;
    row["TypeOfCable"] = item.TypeOfCable;
    row["Accessories"] = item.Accessories;

    dt.Rows.Add(row);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();