2016-08-23 6 views
0

WCF Restサービスを返します。Json、レスポンスを取得します。Json文字列ですが、非直列化後にNullオブジェクトを返します.JsonにはListを含む応答オブジェクトが含まれます。デシリアライズリストの後にヌルNewtonJSを使用してJsonを逆シリアル化します。

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

答えて

0

あなたはこのような二つの追加のクラス(クラスの名前があるが必要

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

はまだあなたが 'returnValue'がnullではないということを確認したヌルリストに –

+0

を与えますか? – Mostafiz

+0

はい、それはヌルではなく、リストにオブジェクトがあります –

1

その後

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

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

別のモデルクラスを作成し、この方法をデシリアライズすることができますを取得します重要ではなく、あなたが好きなものでもかまいませんが、重要な部分はプロパティです)。

その後、

と、このようなものを使用する:あなたはJSON Utilsからあなたのデシリアライゼーションクラスを作成することができます

var root = JsonConvert.DeserializeObject<Root>(returnValue); 
var response = root.GetStudentsListJSONResult.response; 
0

あなたが適切な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); 
関連する問題