code_junkie

JavaScriptSerializer C# and Generics (newbie mistakes I think!)

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. {
  2. "objects": [
  3. {
  4. "categoryid": "1",
  5. "name": "funny",
  6. "serverimageid": "1",
  7. "dateuploaded": "2008-11-17 16:16:41",
  8. "enabled": "1"
  9. },
  10. {
  11. "categoryid": "2",
  12. "name": "happy",
  13. "serverimageid": "2",
  14. "dateuploaded": "2008-11-17 16:17:00",
  15. "enabled": "1"
  16. },
  17. {
  18. "categoryid": "3",
  19. "name": "sad",
  20. "serverimageid": "3",
  21. "dateuploaded": "2008-11-16 16:17:13",
  22. "enabled": "1"
  23. }
  24. ]
  25. }
  26.  
  27. // s is the string
  28. s = s.Remove(0, 11);
  29. // last }
  30. int stringLength = s.Length;
  31. s = s.Remove(stringLength - 1, 1);
  32.  
  33. listOfCategories = serializer1.Deserialize<List<Category>>(s);
  34.  
  35. public class Category
  36. {
  37. public int categoryID;
  38. public string name;
  39. public int imageID;
  40. public DateTime dateUpdated;
  41. public int isActive;
  42. public int displayOrder;
  43. }
  44.  
  45. {
  46. "objects": {
  47. "categories": [
  48. {
  49. "name": "Congratulations",
  50. "imageID": "1",
  51. "isActive": "1",
  52. "displayOrder": "0",
  53. "dateUpdated": "2008-11-27 00:00:00"
  54. },
  55. {
  56. "name": "Animals",
  57. "imageID": "2",
  58. "isActive": "1",
  59. "displayOrder": "0",
  60. "dateUpdated": "2008-11-26 00:00:00"
  61. },
  62. {
  63. "name": "Romance",
  64. "imageID": "3",
  65. "isActive": "1",
  66. "displayOrder": "0",
  67. "dateUpdated": "2008-11-24 00:00:00"
  68. }
  69. ],
  70. "present": [
  71. {
  72. "presentID": "1",
  73. "name": "Tiger",
  74. "categoryID": "2",
  75. "imageID": "1",
  76. "dateUpdated": "2008-11-27",
  77. "isActive": "1",
  78. "isAnimated": null,
  79. "isInteractive": null,
  80. "isAdaptive": null,
  81. "webLinkURL": null
  82. },
  83. {
  84. "giphtID": "2",
  85. "name": "Donkey",
  86. "categoryID": "2",
  87. "imageID": "2",
  88. "dateUpdated": "2008-11-27",
  89. "isActive": "1",
  90. "isAnimated": null,
  91. "isInteractive": null,
  92. "isAdaptive": null,
  93. "webLinkURL": null
  94. },
  95. {
  96. "giphtID": "3",
  97. "name": "Elephant",
  98. "categoryID": "2",
  99. "imageID": "3",
  100. "dateUpdated": "2008-11-27",
  101. "isActive": "1",
  102. "isAnimated": null,
  103. "isInteractive": null,
  104. "isAdaptive": null,
  105. "webLinkURL": null
  106. }
  107. ]
  108. }
  109. }
  110.  
  111. using System;
  112. using System.Collections.Generic;
  113. using System.Web.Script.Serialization;
  114.  
  115. class Program
  116. {
  117. static void Main( string[] args )
  118. {
  119. string json = System.IO.File.ReadAllText( "../../input.json" );
  120.  
  121. var serializer = new JavaScriptSerializer();
  122. Structure jsonStructure = serializer.Deserialize<Structure>( json );
  123. System.Diagnostics.Debugger.Break();
  124. }
  125. }
  126.  
  127. class Structure
  128. {
  129. public StructureObjects objects;
  130. }
  131.  
  132. class StructureObjects
  133. {
  134. public List<StructureCategory> categories;
  135. public List<StructurePresent> present;
  136. }
  137.  
  138. class StructureCategory
  139. {
  140. public string name;
  141. public int imageID;
  142. public DateTime dateUpdated;
  143. public int isActive;
  144. public int displayOrder;
  145. }
  146.  
  147. class StructurePresent
  148. {
  149. public int presentID;
  150. public string name;
  151. public int categoryID;
  152. public int imageID;
  153. public DateTime dateUpdated;
  154. public int isActive;
  155. public int? isAnimated;
  156. public int? isInteractive;
  157. public int? isAdaptive;
  158. public Uri webLinkURL;
  159. }
  160.  
  161. [DataContract]
  162. class Foo
  163. {
  164. [DataMember(Name = "objects")]
  165. public Bar Bar { get; set; }
  166. }
  167.  
  168. [DataContract]
  169. class Bar
  170. {
  171. public Bar() {
  172. Categories = new List<Category>();
  173. Present = new List<Present>();
  174. }
  175. [DataMember(Name = "categories")]
  176. public List<Category> Categories { get; private set; }
  177. [DataMember(Name = "present")]
  178. public List<Present> Present { get; private set; }
  179. }
  180. [DataContract]
  181. class Category
  182. {
  183. [DataMember(Name = "name")]
  184. public string Name {get;set;}
  185. [DataMember(Name = "imageID")]
  186. public int ImageID {get;set;}
  187. [DataMember(Name = "isActive")]
  188. public int IsActive {get;set;}
  189. [DataMember(Name = "displayOrder")]
  190. public int DisplayOrder {get;set;}
  191. [DataMember(Name = "dateUpdated")]
  192. public string DateUpdated {get;set;}
  193. }
  194. [DataContract]
  195. class Present
  196. {
  197. [DataMember(Name = "presentID")]
  198. public int PresentID {get;set;}
  199. [DataMember(Name = "name")]
  200. public string Name {get;set;}
  201. [DataMember(Name = "categoryID")]
  202. public int CategoryID {get;set;}
  203. [DataMember(Name = "imageID")]
  204. public int ImageID {get;set;}
  205. [DataMember(Name = "dateUpdated")]
  206. public string DateUpdated {get;set;}
  207. [DataMember(Name = "isActive")]
  208. public int IsActive {get;set;}
  209. [DataMember(Name = "isAnimated")]
  210. public int? IsAnimated {get;set;}
  211. [DataMember(Name = "isInteractive")]
  212. public int? IsInteractive {get;set;}
  213. [DataMember(Name = "isAdaptive")]
  214. public int? IsAdaptive {get;set;}
  215. [DataMember(Name = "webLinkURL")]
  216. public string WebLinkUrl {get;set;}
  217. }
Add Comment
Please, Sign In to add comment