0

最初我的 json 格式是

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

所以我使用下面的类使用 Newtonsoft.json 进行反序列化

[DataContract]
public class students
{
    [DataMember(Name = "code")]
    public int Code { get; set; }

    [DataMember(Name = "message")]
    public string Message { get; set; }

    [DataMember(Name = "students")]
    public StudentDetail StudentDetail { get; set; }
}
[DataContract]
public class StudentDetail 
{
    [DataMember(Name = "id")]
    public string ID { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "percentage")]
    public double PercentageForEdit { get; set; }

    [DataMember(Name = "type")]
    public string Type { get; set; }
}

但是现在我的json变成了,

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

我应该如何改变我的学生班级,以便,

  StudentDetails = JsonConvert.DeserializeObject<Students>(data);
4

2 回答 2

2

如果您使用 Newtonsoft.Json,请尝试使用该类:

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; }
}

之后,您可以使用该类通过以下方式反序列化响应:

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

PS当然不要忘记[DataContract][DataMember]属性

于 2013-11-05T09:14:41.973 回答
0

首先,您的 Json 似乎无效,对象 {} 缺少括号。要找到您匹配的 C# 类,此站点上有一个不错的转换器

对于带有对象括号的第二个 Json:

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

它建议这些课程:

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; }
}

你可以自己玩这个生成器工具。

于 2013-11-05T07:42:18.457 回答