使用NewtonJS反序列化Json

时间:2016-08-23 05:56:33

标签: c# json wcf json-deserialization

我有WCF Rest服务返回Json,我得到响应Json字符串但是在反序列化之后它给出Null对象,Json包含包含List的响应对象,在反序列化字符串之前json字符串在反序列化后显示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)); 

3 个答案:

答案 0 :(得分:1)

你需要另外两个类这样的类(类的名称并不重要,可以是你喜欢的任何类,重要的部分是属性):

setMenu(null)

然后使用这样的东西:

public class Root
{
    public Result GetStudentsListJSONResult { get; set; }
}

public class Result
{
    public Response response { get; set; }
}

答案 1 :(得分:0)

您可以通过这种方式反序列化,创建单独的模型类

public class NewModel
{
    public Response GetStudentsListJSONResult { get; set; };
}

public class NewModel2
{
    public NewModel Result { get; set; };
}

然后

NewModel2 response = Newtonsoft.Json.JsonConvert.DeserializeObject<NewModel2>(returnValue); 

答案 2 :(得分:0)

您可以从JSON Utils创建De Serialization类,如果您想要正确的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; }
}

使用此方法进行De Serialize,如下所示:

var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(returnValue);