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

时间:2016-12-30 04:13:19

标签: c#

我对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数据,如何反序列化json数据并将它们收集到各自的列表中?我在Windows窗体应用程序中工作。感谢大家的帮助和建议。

从文件中读取json数据

using(StreamReader file = File.OpenText(@ "Data.JSON")) {}

1 个答案:

答案 0 :(得分:0)

您可以使用程序包管理器安装Newtonsoft 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; }
}

然后像这样去除它

string jsonstr = File.ReadAllText("YourJsonFile");
var ser = JsonConvert.DeserializeObject<Rootobject>(jsonstr);

您将获得所有这些数据

foreach(NonPortable np in ser.Non_Portable)
{
    Console.WriteLine(np.Accessories);
    Console.WriteLine(np.DeliveryMode);
    Console.WriteLine(np.NameOfGamingEquipments);
    Console.WriteLine(np.quantityOfCables);
    Console.WriteLine(np.RentalPrice);
    Console.WriteLine(np.ResourceId);
    Console.WriteLine(np.TypeOfCable);
}

修改

我必须在我的作业中使用继承,非便携式类和可移植类将从基类游戏设备继承属性 你的课程看起来像这样

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

仍然反序列化和其他事情将是相同的,没有任何变化。