How to deserialize json data in windows phone?

后端 未结 2 913
醉话见心
醉话见心 2021-01-29 00:35

Initially my json was in the format,

\"code\": 0,
\"message\": \"success\",
\"students\": [
    {
        \"id\": \"257633000000070001\",
        \"name\": \"hje         


        
相关标签:
2条回答
  • 2021-01-29 01:34

    If you using Newtonsoft.Json, try to use that classes:

    public class StudentDetails
    {
        public string id { get; set; }
        public string name { get; set; }
        public int percentage { get; set; }
        public string type { get; set; }
    }
    
    public class Student
    {
        public int code { get; set; }
        public string message { get; set; }
        public List<StudentDetails> students { get; set; }
    }
    

    After that, you can use that class to Deserialize responses using following way:

    var parsedResponse = JsonConvert.DeserializeObject<Student>(data);
    

    P.S. Of course, do not forget about [DataContract] and [DataMember] attributes

    0 讨论(0)
  • 2021-01-29 01:35

    first of all your Json does not seem to be valid, the surrounding brackets are missing for the object { }. To find your matching C# class there is a nice converter on this site

    For your second Json with the object brackets:

    "code": 0,
    "message": "success",
    "students": {
    "details":{
        "hjeke": {
            "id": "257633000000070001",
            "name": "hjeke",
            "percentage": 36,
            "type": "Good",
        },
        "Second": {
            "id": "257633000000073001",
            "name": "Second",
            "percentage": 4,
            "type": "bad",
        }
      }
    }
    

    it suggests these classes:

    public class Hjeke
    {
      public string id { get; set; }
      public string name { get; set; }
      public int percentage { get; set; }
      public string type { get; set; }
    }
    
    public class Second
    {
      public string id { get; set; }
      public string name { get; set; }
      public int percentage { get; set; }
      public string type { get; set; }
    }
    
    public class Details
    {
      public Hjeke hjeke { get; set; }
      public Second Second { get; set; }
    }
    
    public class Students
    {
      public Details details { get; set; }
    }
    
    public class RootObject
    {
      public int code { get; set; }
      public string message { get; set; }
      public Students students { get; set; }
    }
    

    You can play around with this generator tool by yourself.

    0 讨论(0)
提交回复
热议问题