简体   繁体   中英

C# Reading Data from JSON Textfile using streamreader and Deserializing it into respective lists of objects

I am relatively new to c# programming. I need help for these problems that i have been facing in assignment.

 {

     "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
     }]
 }

The above is my JSON File. The assignment requires me to load data from text file hence I am using streamreader to read the data from 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;
   }
  }
 }

Now then I have read the json data from the file, How do I deserialize json data and have them collected to their respective lists ? I am working in windows form application. Thanks for everyone's help and advice in advance.

Reading json data from file

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

You can install Newtonsoft JSON.Net using Package Manager and then you can create the classes for your JSON like this

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

and then desearlize it like this

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

You will get all your data like this

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

Edit

I have to use inheritance in my assignment that non portable class and portable class will inherit properties from the base class gaming equipments Your classes would look like this

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

and still the Deserialization and other things will be same with no changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM