|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.UI; |
| 4 | +using System.Collections.Generic; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | + |
| 8 | +public class JsonNetSample : MonoBehaviour |
| 9 | +{ |
| 10 | + public Text Output; |
| 11 | + |
| 12 | + void Start() |
| 13 | + { |
| 14 | + Output.text = "Start!\n\n"; |
| 15 | + |
| 16 | + SerailizeJson(); |
| 17 | + DeserializeJson(); |
| 18 | + LinqToJson(); |
| 19 | + JsonPath(); |
| 20 | + |
| 21 | + WriteLine("\nDone!"); |
| 22 | + } |
| 23 | + |
| 24 | + void WriteLine(string msg) |
| 25 | + { |
| 26 | + Output.text = Output.text + msg + "\n"; |
| 27 | + } |
| 28 | + |
| 29 | + public class Product |
| 30 | + { |
| 31 | + public string Name; |
| 32 | + public DateTime ExpiryDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 33 | + public decimal Price; |
| 34 | + public string[] Sizes; |
| 35 | + |
| 36 | + public override bool Equals(object obj) |
| 37 | + { |
| 38 | + if (obj is Product) |
| 39 | + { |
| 40 | + Product p = (Product)obj; |
| 41 | + |
| 42 | + return (p.Name == Name && p.ExpiryDate == ExpiryDate && p.Price == Price); |
| 43 | + } |
| 44 | + |
| 45 | + return base.Equals(obj); |
| 46 | + } |
| 47 | + |
| 48 | + public override int GetHashCode() |
| 49 | + { |
| 50 | + return (Name ?? string.Empty).GetHashCode(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void SerailizeJson() |
| 55 | + { |
| 56 | + WriteLine("* SerailizeJson"); |
| 57 | + |
| 58 | + Product product = new Product(); |
| 59 | + product.Name = "Apple"; |
| 60 | + product.ExpiryDate = new DateTime(2008, 12, 28); |
| 61 | + product.Sizes = new string[] { "Small" }; |
| 62 | + |
| 63 | + string json = JsonConvert.SerializeObject(product); |
| 64 | + WriteLine(json); |
| 65 | + } |
| 66 | + |
| 67 | + public class Movie |
| 68 | + { |
| 69 | + public string Name { get; set; } |
| 70 | + public string Description { get; set; } |
| 71 | + public string Classification { get; set; } |
| 72 | + public string Studio { get; set; } |
| 73 | + public DateTime? ReleaseDate { get; set; } |
| 74 | + public List<string> ReleaseCountries { get; set; } |
| 75 | + } |
| 76 | + |
| 77 | + void DeserializeJson() |
| 78 | + { |
| 79 | + WriteLine("* DeserializeJson"); |
| 80 | + |
| 81 | + string json = @"{ |
| 82 | + 'Name': 'Bad Boys', |
| 83 | + 'ReleaseDate': '1995-4-7T00:00:00', |
| 84 | + 'Genres': [ |
| 85 | + 'Action', |
| 86 | + 'Comedy' |
| 87 | + ] |
| 88 | + }"; |
| 89 | + |
| 90 | + Movie m = JsonConvert.DeserializeObject<Movie>(json); |
| 91 | + |
| 92 | + string name = m.Name; |
| 93 | + WriteLine(name); |
| 94 | + } |
| 95 | + |
| 96 | + void LinqToJson() |
| 97 | + { |
| 98 | + WriteLine("* LinqToJson"); |
| 99 | + |
| 100 | + JArray array = new JArray(); |
| 101 | + array.Add("Manual text"); |
| 102 | + array.Add(new DateTime(2000, 5, 23)); |
| 103 | + |
| 104 | + JObject o = new JObject(); |
| 105 | + o["MyArray"] = array; |
| 106 | + |
| 107 | + string json = o.ToString(); |
| 108 | + WriteLine(json); |
| 109 | + } |
| 110 | + |
| 111 | + private void JsonPath() |
| 112 | + { |
| 113 | + WriteLine("* JsonPath"); |
| 114 | + |
| 115 | + var o = JObject.Parse(@"{ |
| 116 | + 'Stores': [ |
| 117 | + 'Lambton Quay', |
| 118 | + 'Willis Street' |
| 119 | + ], |
| 120 | + 'Manufacturers': [ |
| 121 | + { |
| 122 | + 'Name': 'Acme Co', |
| 123 | + 'Products': [ |
| 124 | + { |
| 125 | + 'Name': 'Anvil', |
| 126 | + 'Price': 50 |
| 127 | + } |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + 'Name': 'Contoso', |
| 132 | + 'Products': [ |
| 133 | + { |
| 134 | + 'Name': 'Elbow Grease', |
| 135 | + 'Price': 99.95 |
| 136 | + }, |
| 137 | + { |
| 138 | + 'Name': 'Headlight Fluid', |
| 139 | + 'Price': 4 |
| 140 | + } |
| 141 | + ] |
| 142 | + } |
| 143 | + ] |
| 144 | + }"); |
| 145 | + |
| 146 | + JToken acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]"); |
| 147 | + WriteLine(acme.ToString()); |
| 148 | + |
| 149 | + IEnumerable<JToken> pricyProducts = o.SelectTokens("$..Products[?(@.Price >= 50)].Name"); |
| 150 | + foreach (var item in pricyProducts) |
| 151 | + { |
| 152 | + WriteLine(item.ToString()); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments