2016-08-23 15 views
0

我有WCF REST服务返回JSON,我得到的回应JSON字符串但经过反序列它给空对象,JSON包含其中包含列表响应对象,前反序列串JSON字符串显示3个DTOStundet对象反序列列表显示空后反序列化的Json使用NewtonJS

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

这就是IM反序列化JSON之后这种反应得到空

Response response = (Response)Newtonsoft.Json.JsonConvert.DeserializeObject(returnValue, typeof(Response)); 

回答

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); 
+0

仍然为空列表 –

+0

你检查它,你的'returnValue'不为空? – Mostafiz

+0

是的,它不是空它有列表中的对象 –

1

您需要两个额外的类像这样(的类名是并不重要,可以是你喜欢的,重要的部分是属性):

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

您可以从JSON Utils创建德序列化程序类,你也可以添加Json的属性,如果你想为正确的C#命名。

请找到你的类生成:

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