Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用streamreader从JSON文本文件读取数据,并将其反序列化为相应的对象列表_C# - Fatal编程技术网

C#使用streamreader从JSON文本文件读取数据,并将其反序列化为相应的对象列表

C#使用streamreader从JSON文本文件读取数据,并将其反序列化为相应的对象列表,c#,C#,我对c#编程比较陌生。我在作业中遇到的这些问题需要帮助 { "Non_Portable": [{ "NameOfGamingEquipments": "Playstation 4", "ResourceId": 1, "RentalPrice": 200, "DeliveryMode": "Hand Carry", "quantityOfCables":

我对c#编程比较陌生。我在作业中遇到的这些问题需要帮助

 {

     "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文件。分配要求我从文本文件加载数据,因此我使用streamreader从json读取数据

public class Resource {
 public static List < UserAccount > UserAccounts {
   get;
   set;
  } //container     for useraccounts
 public List < Non_Portable > Non_PortableEquip {
  get;
  set;
 }
 public List < Portable > PortableEquip {
  get;
  set;
 }
 public List < Booking > Bookings {
  get;
  set;
 }
 public List < BookingDetail > BookingDetails {
  get;
  set;
 }
 public abstract class GamingEquipment {
  public string NameOfGamingEquipments

  {
   get;
   set;
  }


  public int ResourceId

  {
   get;
   set;
  }



  public double RentalPrice {
   get;
   set;
  }


  public string DeliveryMode

  {
   get;
   set;
  }


 }

 public class Non_Portable: GamingEquipment {


  public int quantityOfCables

  {
   get;
   set;
  }

  public string TypeOfCable

  {
   get;
   set;
  }

  public string Accessories

  {
   get;
   set;
  }



 }



 public class Portable: GamingEquipment {
  public string sizeOfScreen

  {
   get;
   set;
  }

  public double quantityOfCartridges

  {
   get;
   set;
  }

  public string CartridgeName

  {
   get;
   set;
  }

  public bool touchScreenFunction

  {
   get;
   set;
  }


 }

 public class UserAccount {
  private string userName;
  private string passWord;
  private string name;
  private string email;
  private int cardNum;
  private DateTime expiryDate;
  private string paymentType;
  private string address;
  public string Username {
   set {
    userName = value;
   }

   get {
    return userName;
   }
  }

  public string Password {
   set {
    passWord = value;
   }

   get {
    return passWord;
   }
  }


  public string Name {
   set {
    name = value;
   }

   get {
    return name;
   }
  }


  public string Email {
   set {
    email = value;
   }
   get {
    return email;
   }
  }


  public int CardNum {
   set {
    cardNum = value;
   }
   get {
    return cardNum;
   }
  }

  public DateTime ExpiryDate {
   set {
    expiryDate = value;
   }
   get {
    return expiryDate;
   }
  }


  public string PaymentType {
   set {
    paymentType = value;
   }
   get {
    return paymentType;
   }
  }


  public string Address {
   set {
    address = value;
   }
   get {
    return address;
   }
  }
 }
您可以使用包管理器安装JSON.Net,然后可以像下面这样为JSON创建类

public class NonPortable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("quantityOfCables")]
    public int quantityOfCables { get; set; }
    [JsonProperty("TypeOfCable")]
    public string TypeOfCable { get; set; }
    [JsonProperty("Accessories")]
    public string Accessories { get; set; }
}

public class Portable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("sizeOfScreen")]
    public string sizeOfScreen { get; set; }
    [JsonProperty("quantityOfCartridges")]
    public int quantityOfCartridges { get; set; }
    [JsonProperty("CartridgeName")]
    public string CartridgeName { get; set; }
    [JsonProperty("touchScreenFunction")]
    public bool touchScreenFunction { get; set; }
}

public class Rootobject
{
    [JsonProperty("Non_Portable")]
    public IList<NonPortable> Non_Portable { get; set; }
    [JsonProperty("Portable")]
    public IList<Portable> Portable { get; set; }
}
public abstract class GamingEquipment
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public double RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
}
public class NonPortable : GamingEquipment
{
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable : GamingEquipment
{
    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; }
}
编辑

我必须在我的任务中使用继承,非可移植类和可移植类将继承基类游戏设备的属性 你的课程应该是这样的

public class NonPortable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("quantityOfCables")]
    public int quantityOfCables { get; set; }
    [JsonProperty("TypeOfCable")]
    public string TypeOfCable { get; set; }
    [JsonProperty("Accessories")]
    public string Accessories { get; set; }
}

public class Portable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("sizeOfScreen")]
    public string sizeOfScreen { get; set; }
    [JsonProperty("quantityOfCartridges")]
    public int quantityOfCartridges { get; set; }
    [JsonProperty("CartridgeName")]
    public string CartridgeName { get; set; }
    [JsonProperty("touchScreenFunction")]
    public bool touchScreenFunction { get; set; }
}

public class Rootobject
{
    [JsonProperty("Non_Portable")]
    public IList<NonPortable> Non_Portable { get; set; }
    [JsonProperty("Portable")]
    public IList<Portable> Portable { get; set; }
}
public abstract class GamingEquipment
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public double RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
}
public class NonPortable : GamingEquipment
{
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable : GamingEquipment
{
    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;}
公共双租金价格{get;set;}
公共字符串传递模式{get;set;}
}
公共级非便携设备:游戏设备
{
public int quantityOfCables{get;set;}
公共字符串TypeOfTable{get;set;}
公共字符串附件{get;set;}
}
公共级便携式:游戏设备
{
公共字符串大小屏幕{get;set;}
公共int-quantityOfCartridges{get;set;}
公共字符串CartridgeName{get;set;}
公共布尔触摸屏功能{get;set;}
}
公共类根对象
{
公共列表不可移植{get;set;}
公共列表可移植{get;set;}
}
反序列化和其他内容仍然会保持不变。

您可以使用包管理器安装JSON.Net,然后可以像这样为JSON创建类

public class NonPortable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("quantityOfCables")]
    public int quantityOfCables { get; set; }
    [JsonProperty("TypeOfCable")]
    public string TypeOfCable { get; set; }
    [JsonProperty("Accessories")]
    public string Accessories { get; set; }
}

public class Portable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("sizeOfScreen")]
    public string sizeOfScreen { get; set; }
    [JsonProperty("quantityOfCartridges")]
    public int quantityOfCartridges { get; set; }
    [JsonProperty("CartridgeName")]
    public string CartridgeName { get; set; }
    [JsonProperty("touchScreenFunction")]
    public bool touchScreenFunction { get; set; }
}

public class Rootobject
{
    [JsonProperty("Non_Portable")]
    public IList<NonPortable> Non_Portable { get; set; }
    [JsonProperty("Portable")]
    public IList<Portable> Portable { get; set; }
}
public abstract class GamingEquipment
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public double RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
}
public class NonPortable : GamingEquipment
{
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable : GamingEquipment
{
    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; }
}
编辑

我必须在我的任务中使用继承,非可移植类和可移植类将继承基类游戏设备的属性 你的课程应该是这样的

public class NonPortable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("quantityOfCables")]
    public int quantityOfCables { get; set; }
    [JsonProperty("TypeOfCable")]
    public string TypeOfCable { get; set; }
    [JsonProperty("Accessories")]
    public string Accessories { get; set; }
}

public class Portable
{
    [JsonProperty("NameOfGamingEquipments")]
    public string NameOfGamingEquipments { get; set; }
    [JsonProperty("ResourceId")]
    public int ResourceId { get; set; }
    [JsonProperty("RentalPrice")]
    public int RentalPrice { get; set; }
    [JsonProperty("DeliveryMode")]
    public string DeliveryMode { get; set; }
    [JsonProperty("sizeOfScreen")]
    public string sizeOfScreen { get; set; }
    [JsonProperty("quantityOfCartridges")]
    public int quantityOfCartridges { get; set; }
    [JsonProperty("CartridgeName")]
    public string CartridgeName { get; set; }
    [JsonProperty("touchScreenFunction")]
    public bool touchScreenFunction { get; set; }
}

public class Rootobject
{
    [JsonProperty("Non_Portable")]
    public IList<NonPortable> Non_Portable { get; set; }
    [JsonProperty("Portable")]
    public IList<Portable> Portable { get; set; }
}
public abstract class GamingEquipment
{
    public string NameOfGamingEquipments { get; set; }
    public int ResourceId { get; set; }
    public double RentalPrice { get; set; }
    public string DeliveryMode { get; set; }
}
public class NonPortable : GamingEquipment
{
    public int quantityOfCables { get; set; }
    public string TypeOfCable { get; set; }
    public string Accessories { get; set; }
}

public class Portable : GamingEquipment
{
    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;}
公共双租金价格{get;set;}
公共字符串传递模式{get;set;}
}
公共级非便携设备:游戏设备
{
public int quantityOfCables{get;set;}
公共字符串TypeOfTable{get;set;}
公共字符串附件{get;set;}
}
公共级便携式:游戏设备
{
公共字符串大小屏幕{get;set;}
公共int-quantityOfCartridges{get;set;}
公共字符串CartridgeName{get;set;}
公共布尔触摸屏功能{get;set;}
}
公共类根对象
{
公共列表不可移植{get;set;}
公共列表可移植{get;set;}
}


反序列化和其他内容仍将保持不变。

我很迷茫,希望您能给我一些解决问题的建议。非常感谢!UserAccount、Non_Portable、Portable等的结构是什么?请尝试使用json.net Library@DarkKnight的可能副本,我已经更新了它。问题是,我需要在c#类中使用继承,我担心一旦我在rootobject下组织列表不可移植项和列表可移植项以及组,它就会产生影响,因为我的作业要求我使用继承。我非常迷茫,希望你能给我一些如何处理这个问题的建议。非常感谢!UserAccount、Non_Portable、Portable等的结构是什么?请尝试使用json.net Library@DarkKnight的可能副本,我已经更新了它。问题是我需要在c#类中使用继承,我担心一旦我在rootobject下组织列表不可移植项和列表可移植项以及组,它就会影响,因为我的作业要求我使用继承谢谢你的帮助。我考虑使用这些,但是我必须在我的作业中使用继承,非便携类和便携类将继承基类游戏设备的属性。公共抽象类GAMING设备{公共字符串NAMEOFGAMING设备{GET;SET;}公共int资源库{get;set;}。public double RentalPrice{get;set;}public string DeliveryMode{get;set;}}公共类非便携:游戏设备{}公共类便携:游戏设备{查看更新的答案/解决方案@SofiaWongCould找不到文件“C:\Application Development Project\APPD_Assignment\APPD_Assignment\bin\Debug\Data.JSON”。字符串jsonstr=file.ReadAllText(“Data.JSON”);这是我的代码,但我有一个错误。找不到文件“C:\Application Development Project\APPD\u Assignment\APPD\u Assignment\bin\Debug\Data.JSON”。我有这个错误。谢谢你的帮助。我考虑使用这些,但是我必须在我的作业中使用继承,非便携类和便携类将继承基类游戏设备的属性。公共抽象类GAMING设备{公共字符串NAMEOFGAMING设备{GET;SET;}公共int资源库{get;set;}。public double RentalPrice{get;set;}public string DeliveryMode{get;set;}}公共类非便携:游戏设备{}公共类便携:游戏设备{查看更新的答案/解决方案@SofiaWongCould找不到文件“C:\Application Development Project\APPD_Assignment\APPD_Assignment\bin\Debug\Data.JSON”。字符串jsonstr=file.ReadAllText(“Data.JSON”);这是我的代码,但我有一个错误。找不到文件“C:\Application Development Project\APPD\u Assignment\APPD\u Assignment\bin\Debug\Data.JSON”。我有这个错误。