2013-04-06 168 views
0

我以前成功地使用過Newtonsoft.Json來反序列化JSON,但是我遇到了這個簡單例子的問題 - 請看下面 - 反序列化方法不會失敗但它沒有實例化具有期望值的「ServiceResponse」類。調試器顯示: ServiceResponse.StatusInfo = NULL, ServiceResponse.Email = NULL, ServiceResponse.JobId = 0Newtonsoft.Json:JsonConvert.DeserializeObject - 沒有結果沒有錯誤

// Generated by Xamasoft JSON Class Generator 
// http://www.xamasoft.com/json-class-generator 

using System; 
using System.Collections.Generic; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
using Example.fmeResponseJsonTypes; 

namespace Example.fmeResponseJsonTypes 
{ 

    public class StatusInfo 
    { 

     [JsonProperty("mode")] 
     public string Mode { get; set; } 

     [JsonProperty("status")] 
     public string Status { get; set; } 
    } 

    public class ServiceResponse 
    { 

     [JsonProperty("statusInfo")] 
     public StatusInfo StatusInfo { get; set; } 

     [JsonProperty("email")] 
     public string Email { get; set; } 

     [JsonProperty("jobID")] 
     public int JobID { get; set; } 
    } 

} 

namespace Example 
{ 

    public class fmeResponse 
    { 

     [JsonProperty("serviceResponse")] 
     public ServiceResponse ServiceResponse { get; set; } 

     public void Deserialize() 
     { 
      string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"[email protected]\",\"jobID\": 73}}"; 

      ServiceResponse serviceResponse = null; 

      try 
      { 
       serviceResponse = JsonConvert.DeserializeObject<ServiceResponse>(res); 
      } 
      catch (Exception e) 
      { 
       throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace); 
      } 

     } 

    } 

} 

回答

0

試試這個, 請加JsonProperty,我做到了急。

如果您不確定JsonString所需的類,請嘗試json2csharp。它有時非常有用。

using System; 
using System.Collections.Generic; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
using Example.fmeResponseJsonTypes; 

namespace Example.fmeResponseJsonTypes 
{ 
public class StatusInfo 
{ 
    public string mode { get; set; } 
    public string status { get; set; } 
} 

public class ServiceResponse 
{ 
    public StatusInfo statusInfo { get; set; } 
    public string email { get; set; } 
    public int jobID { get; set; } 
} 

public class RootObject 
{ 
    public ServiceResponse serviceResponse { get; set; } 
} 

} 

namespace Example 
{ 

public class fmeResponse 
{ 

    public RootObject RootObject { get; set; } 

    public void Deserialize() 
    { 
     string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"[email protected]\",\"jobID\": 73}}"; 

     ServiceResponse serviceResponse = null; 
     RootObject rootObject = null; 

     try 
     { 
      rootObject = JsonConvert.DeserializeObject<RootObject>(res); 
     } 
     catch (Exception e) 
     { 
      throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace); 
     } 
    } 
} 
+0

謝謝! (我的錯!) – Tor 2013-04-07 12:47:26

相關問題