code_junkie

Deserializing JSON objects as List<type> not working with asmx service

Nov 13th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class person
  2. {
  3. public person()
  4. {
  5. //
  6. // TODO: Add constructor logic here
  7. //
  8. }
  9.  
  10. public int seq
  11. {
  12. get;
  13. set;
  14. }
  15.  
  16. public string firstName
  17. {
  18. get;
  19. set;
  20. }
  21. public string lastName
  22. {
  23. get;
  24. set;
  25. }
  26.  
  27. }
  28.  
  29. [{"seq":1,"firstName":"Chris","lastName":"Westbrook"},
  30. {"seq":2,"firstName":"sayyl","lastName":"westbrook"}]
  31.  
  32. [WebMethod]
  33. public void updatePeople(string json)
  34. {
  35. IList<person> people =
  36. new JavaScriptSerializer().Deserialize<IList<person>>(json);
  37.  
  38. //do stuff...
  39. }
  40.  
  41. var person=new object;
  42. person.firstName="chris";
  43. person.lastName="Westbrook";
  44. person.seq=-1;
  45. var data=new object;
  46. data.p=person;
  47. JSON.stringify(data);
  48.  
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Web.Script.Serialization;
  52.  
  53. namespace Test
  54. {
  55. class Program
  56. {
  57. class Person
  58. {
  59. public int SequenceNumber { get; set; }
  60. public string FirstName { get; set; }
  61. public string LastName { get; set; }
  62. }
  63.  
  64. public static void Main()
  65. {
  66. string json = "[{"SequenceNumber":1,"FirstName":"FN1","LastName":"LN1"},{"SequenceNumber":2,"FirstName":"FN2","LastName":"LN2"}]";
  67. IList<Person> persons = new JavaScriptSerializer()
  68. .Deserialize<IList<Person>>(json);
  69. Console.WriteLine(persons.Count);
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment