JsonNetSample.cs 4.1 KB

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