使用 NewtonJS 反序列化 Json
Deserialize Json Using NewtonJS
我有 WCF Rest 服务 Returns Json ,我得到响应 Json 字符串但是在反序列化后它给出 Null 对象,Json 包含响应对象包含 List ,在反序列化字符串之前 json 字符串在反序列化列表显示 null
之后显示 3 个 DTOStundet 对象
string returnValue = Navigator.GET(url, APIHearderCollection);
{
"GetStudentsListJSONResult":
{
"response":
{
"DTOStudentList":[
{
"Address":"Kandy",
"Age":20,
"CourseName":"Physical Sience",
"DateOfBirth":"\/Date(318191400000+0530)\/",
"StudentId":1,"StudentName":"Kumar Sangakkara",
"TelePhoneNumber":"071975769"
},
{
"Address":"Colombo",
"Age":21,"CourseName":"Physical Sience",
"DateOfBirth":"\/Date(2658600000+0530)\/",
"StudentId":2,"StudentName":"Mahela Jayawardena",
"TelePhoneNumber":"071975759"
}
],
"ResponseStatus":0
}
}
}
returnValue 包含要反序列化的 json 字符串
图片说明在这里]1
这是我在此响应为空后反序列化 json 的地方
Response response = (Response)Newtonsoft.Json.JsonConvert.DeserializeObject(returnValue, typeof(Response));
你可以这样反序列化,创建单独的模型class
public class NewModel
{
public Response GetStudentsListJSONResult { get; set; };
}
public class NewModel2
{
public NewModel Result { get; set; };
}
然后
NewModel2 response = Newtonsoft.Json.JsonConvert.DeserializeObject<NewModel2>(returnValue);
你需要两个额外的 类 像这样(类 的名称并不重要,可以随意,重要的部分是属性):
public class Root
{
public Result GetStudentsListJSONResult { get; set; }
}
public class Result
{
public Response response { get; set; }
}
然后使用类似这样的东西:
var root = JsonConvert.DeserializeObject<Root>(returnValue);
var response = root.GetStudentsListJSONResult.response;
您可以从 JSON Utils 创建反序列化 类,如果您想要正确的 C# 命名,也可以添加 Json 属性。
请查找您的 类 生成的:
public class DTOStudentList
{
[JsonProperty("Address")]
public string Address { get; set; }
[JsonProperty("Age")]
public int Age { get; set; }
[JsonProperty("CourseName")]
public string CourseName { get; set; }
[JsonProperty("DateOfBirth")]
public DateTime DateOfBirth { get; set; }
[JsonProperty("StudentId")]
public int StudentId { get; set; }
[JsonProperty("StudentName")]
public string StudentName { get; set; }
[JsonProperty("TelePhoneNumber")]
public string TelePhoneNumber { get; set; }
}
public class Response
{
[JsonProperty("DTOStudentList")]
public IList<DTOStudentList> DTOStudentList { get; set; }
[JsonProperty("ResponseStatus")]
public int ResponseStatus { get; set; }
}
public class GetStudentsListJSONResult
{
[JsonProperty("response")]
public Response Response { get; set; }
}
public class RootObject
{
[JsonProperty("GetStudentsListJSONResult")]
public GetStudentsListJSONResult GetStudentsListJSONResult { get; set; }
}
使用它反序列化如下:
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(returnValue);
我有 WCF Rest 服务 Returns Json ,我得到响应 Json 字符串但是在反序列化后它给出 Null 对象,Json 包含响应对象包含 List ,在反序列化字符串之前 json 字符串在反序列化列表显示 null
之后显示 3 个 DTOStundet 对象string returnValue = Navigator.GET(url, APIHearderCollection);
{
"GetStudentsListJSONResult":
{
"response":
{
"DTOStudentList":[
{
"Address":"Kandy",
"Age":20,
"CourseName":"Physical Sience",
"DateOfBirth":"\/Date(318191400000+0530)\/",
"StudentId":1,"StudentName":"Kumar Sangakkara",
"TelePhoneNumber":"071975769"
},
{
"Address":"Colombo",
"Age":21,"CourseName":"Physical Sience",
"DateOfBirth":"\/Date(2658600000+0530)\/",
"StudentId":2,"StudentName":"Mahela Jayawardena",
"TelePhoneNumber":"071975759"
}
],
"ResponseStatus":0
}
}
}
returnValue 包含要反序列化的 json 字符串
图片说明在这里]1
这是我在此响应为空后反序列化 json 的地方
Response response = (Response)Newtonsoft.Json.JsonConvert.DeserializeObject(returnValue, typeof(Response));
你可以这样反序列化,创建单独的模型class
public class NewModel
{
public Response GetStudentsListJSONResult { get; set; };
}
public class NewModel2
{
public NewModel Result { get; set; };
}
然后
NewModel2 response = Newtonsoft.Json.JsonConvert.DeserializeObject<NewModel2>(returnValue);
你需要两个额外的 类 像这样(类 的名称并不重要,可以随意,重要的部分是属性):
public class Root
{
public Result GetStudentsListJSONResult { get; set; }
}
public class Result
{
public Response response { get; set; }
}
然后使用类似这样的东西:
var root = JsonConvert.DeserializeObject<Root>(returnValue);
var response = root.GetStudentsListJSONResult.response;
您可以从 JSON Utils 创建反序列化 类,如果您想要正确的 C# 命名,也可以添加 Json 属性。
请查找您的 类 生成的:
public class DTOStudentList
{
[JsonProperty("Address")]
public string Address { get; set; }
[JsonProperty("Age")]
public int Age { get; set; }
[JsonProperty("CourseName")]
public string CourseName { get; set; }
[JsonProperty("DateOfBirth")]
public DateTime DateOfBirth { get; set; }
[JsonProperty("StudentId")]
public int StudentId { get; set; }
[JsonProperty("StudentName")]
public string StudentName { get; set; }
[JsonProperty("TelePhoneNumber")]
public string TelePhoneNumber { get; set; }
}
public class Response
{
[JsonProperty("DTOStudentList")]
public IList<DTOStudentList> DTOStudentList { get; set; }
[JsonProperty("ResponseStatus")]
public int ResponseStatus { get; set; }
}
public class GetStudentsListJSONResult
{
[JsonProperty("response")]
public Response Response { get; set; }
}
public class RootObject
{
[JsonProperty("GetStudentsListJSONResult")]
public GetStudentsListJSONResult GetStudentsListJSONResult { get; set; }
}
使用它反序列化如下:
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(returnValue);