|
| 1 | +using LearningCSharp.Topics.EF.Sample; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace LearningCSharp.Cmd.Series5 |
| 10 | +{ |
| 11 | + public class Tester |
| 12 | + { |
| 13 | + public static void Main() |
| 14 | + { |
| 15 | + |
| 16 | + var itemList = ProductManager.GetProductList("water"); |
| 17 | + |
| 18 | + foreach (var item in itemList) |
| 19 | + { |
| 20 | + Console.WriteLine($"Id: {item.Id}, Name: {item.Name}, Price: {item.Price}"); |
| 21 | + } |
| 22 | + |
| 23 | + Console.Read(); |
| 24 | + var personObj = new Person() |
| 25 | + { |
| 26 | + LastName = "Aremu", |
| 27 | + FirstName = "Lollipop", |
| 28 | + Addresses = new List<Address>() |
| 29 | + { |
| 30 | + new Address() |
| 31 | + { |
| 32 | + City = "VI", |
| 33 | + Street = null |
| 34 | + }, |
| 35 | + new Address() |
| 36 | + { |
| 37 | + City = "Awoyaya", |
| 38 | + Street = "Convenant Drive" |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + var serializedJson = JsonConvert.SerializeObject(personObj); |
| 45 | + |
| 46 | + Console.WriteLine(serializedJson); |
| 47 | + |
| 48 | + Console.Read(); |
| 49 | + |
| 50 | + var person = @"{ |
| 51 | + 'LastName': 'Prolifik', |
| 52 | + 'FirstName': 'Lexzy', |
| 53 | + 'Addresses': [ |
| 54 | + {'City': 'VI', 'Street': ''}, |
| 55 | + {'City': 'Awoyaya', 'Street': 'Convenant Drive'}] |
| 56 | + }"; |
| 57 | + |
| 58 | + var objPerson = JsonConvert.DeserializeObject<Person>(person); |
| 59 | + |
| 60 | + Console.WriteLine($"LastName: {objPerson?.LastName} FirstName: {objPerson?.FirstName}"); |
| 61 | + |
| 62 | + foreach (var item in objPerson.Addresses) |
| 63 | + { |
| 64 | + Console.WriteLine($"City: {item.City} Street: {item.Street ?? "NA"}"); |
| 65 | + } |
| 66 | + |
| 67 | + Console.Read(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public class Person |
| 72 | + { |
| 73 | + public string LastName { get; set; } |
| 74 | + public string FirstName { get; set; } |
| 75 | + public List<Address> Addresses { get; set; } |
| 76 | + } |
| 77 | + |
| 78 | + public class Address |
| 79 | + { |
| 80 | + public string City { get; set; } |
| 81 | + public string Street { get; set; } |
| 82 | + } |
| 83 | +} |
0 commit comments