[英]How to deserialize json data in windows phone?
Initially my json was in the format, 最初我的json是格式的,
"code": 0,
"message": "success",
"students": [
{
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good",
},
{
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad",
}]
And so i used the following class for deserializing using Newtonsoft.json 所以我使用以下类使用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; }
}
But now my json is changed to, 但现在我的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",
}
}
}
How should i change my students class so that, 我应该如何改变我的学生班级,
StudentDetails = JsonConvert.DeserializeObject<Students>(data);
If you using Newtonsoft.Json, try to use that classes: 如果您使用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; }
}
After that, you can use that class to Deserialize responses using following way: 之后,您可以使用该类来使用以下方式反序列化响应:
var parsedResponse = JsonConvert.DeserializeObject<Student>(data);
PS Of course, do not forget about [DataContract]
and [DataMember]
attributes PS当然,不要忘记
[DataContract]
和[DataMember]
属性
first of all your Json does not seem to be valid, the surrounding brackets are missing for the object { }. 首先你的Json似乎没有效果,对象{}缺少周围的括号。 To find your matching C# class there is a nice converter on this site
要找到匹配的C#类,此站点上有一个很好的转换器
For your second Json with the object brackets: 对于你的第二个带有对象括号的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",
}
}
}
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. 您可以自己使用这个生成器工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.