C# 显示RootObject类并在datatable中显示它

C# 显示RootObject类并在datatable中显示它,c#,json,datatable,C#,Json,Datatable,我是一个初学者,我需要帮助显示包含2个对象列表的rootobject类,并将它们显示在datatable中。 我一直在想办法,但没有一个能帮我 如何访问RootObject类中的2个列表并使它们显示在datatable中 下面是我的JSON文件 { "Non_Portable": [ { "NameOfGamingEquipments": "Playstation 4", "ResourceId": 1, "RentalPrice": 200, "Delive

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

如何访问RootObject类中的2个列表并使它们显示在datatable中

下面是我的JSON文件

{
"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
  }
]
}
下面是我将json数据反序列化到rootobject的代码

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

    }

我不确定为什么没有显示该表。

我假定您的反序列化成功。然后,您可以按如下方式填充数据表:

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();
var json=File.ReadAllText(@“C:\Path\json.txt”);
var test=JsonConvert.DeserializeObject(json);
DataTable dt=新的DataTable();
dt.列。添加(“包装设备名称”);
添加(“资源ID”);
dt.列添加(“租金价格”);
dt.Columns.Add(“DeliveryMode”);
dt.列。添加(“电缆数量”);
dt.列。添加(“电缆类型”);
dt.列。添加(“附件”);
foreach(测试中的var项目。不可移植)
{
var row=dt.NewRow();
行[“设备名称”]=项。设备名称;
行[“ResourceId”]=Convert.ToString(item.ResourceId);
行[“租金价格”]=item.RentalPrice;
行[“DeliveryMode”]=item.DeliveryMode;
行[“quantityOfCables”]=item.quantityOfCables;
行[“TypeOfCable”]=item.TypeOfCable;
行[“附件”]=项目附件;
dt.行。添加(行);
}
this.GridView1.DataSource=dt;
this.GridView1.DataBind();
对其他列表(便携式)执行相同操作。 结果是:

根对象类:

public class NonPortable
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public int RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public int RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
    public string sizeOfScreen { get; set; }
    public int quantityOfCartridges { get; set; }
    public string CartridgeName { get; set; }
    public bool touchScreenFunction { get; set; }
}

public class RootObject
{
    public List<NonPortable> Non_Portable { get; set; }
    public List<Portable> Portable { get; set; }
}
公共类不可移植
{
网格设备的公共字符串名称{get;set;}
public int ResourceId{get;set;}
公共int RentalPrice{get;set;}
公共字符串传递模式{get;set;}
public int quantityOfCables{get;set;}
公共字符串TypeOfTable{get;set;}
公共字符串附件{get;set;}
}
公共类便携式
{
网格设备的公共字符串名称{get;set;}
public int ResourceId{get;set;}
公共int RentalPrice{get;set;}
公共字符串传递模式{get;set;}
公共字符串大小屏幕{get;set;}
公共int-quantityOfCartridges{get;set;}
公共字符串CartridgeName{get;set;}
公共布尔触摸屏功能{get;set;}
}
公共类根对象
{
公共列表不可移植{get;set;}
公共列表可移植{get;set;}
}

您是否使用VS2015的特殊粘贴创建了Rootobject?您的意思是这个datatable:或某种UI/@federicoscamuzzi没有使用特殊粘贴。因为我使用继承,因为我的任务也需要我。所以我只是在公共类rootobject中手工编码。我使用的是VS2015。@zaitsman,是的,这是您提供的链接。我使用了,并添加了一个附加代码,即dataGridView1.DataSource=dt;GridView需要数据绑定,你忘了吗?数据表填充了吗?哦,我绑定了。现在的问题是大部分json文本数据都不显示。只有租赁价格和资源id的数值。我确信我的json已正确反序列化,并且在调试时没有错误。不确定为什么会显示某些json数据,为什么会显示某些json数据?是否将所有列添加到datarow?我已经在代码中添加了其中三个,您应该添加其他的。
public class NonPortable
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public int RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public int RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
    public string sizeOfScreen { get; set; }
    public int quantityOfCartridges { get; set; }
    public string CartridgeName { get; set; }
    public bool touchScreenFunction { get; set; }
}

public class RootObject
{
    public List<NonPortable> Non_Portable { get; set; }
    public List<Portable> Portable { get; set; }
}