Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在windows phone中反序列化json数据?_C#_Json_Windows Phone - Fatal编程技术网

C# 如何在windows phone中反序列化json数据?

C# 如何在windows phone中反序列化json数据?,c#,json,windows-phone,C#,Json,Windows Phone,最初我的json格式是 "code": 0, "message": "success", "students": [ { "id": "257633000000070001", "name": "hjeke", "percentage": 36, "type": "Good", }, { "id": "257633000000073001", "name": "Second",

最初我的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);
StudentDetails=JsonConvert.DeserializeObject(数据);

首先,您的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; }
}

您可以自己使用此生成器工具。

如果您使用的是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; }
}
公共课堂学生详细信息
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共整数百分比{get;set;}
公共字符串类型{get;set;}
}
公立班学生
{
公共整数代码{get;set;}
公共字符串消息{get;set;}
公共列表学生{get;set;}
}
之后,您可以使用该类通过以下方式反序列化响应:

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

当然,别忘了
[DataContract]
[DataMember]
属性

供参考,比如hjeke和Second,Details类中有大约100个对象。我刚刚测试了该解决方案,即使没有DataContract属性,它也可以正常工作。