2012-10-03 54 views
2

我有一個JSON編碼的字符串。它看起來像這樣:C#Json.Net - 如何反序列化複雜的對象

{ 「jsonrpc」: 「2.0」, 「方法」: 「測試」, 「PARAMS」:[ 「80851851709e01bc9453cced585a7bca」, 「這個」], 「ID」:3}

出於某種原因,我無法使用C#訪問'params'。該對象返回null。這裏是我使用的代碼:

public class Access : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public string EntryMethod(string json) 
    { 
     Requests d = JsonConvert.DeserializeObject<Requests>(json); 
     return "done"; 
    } 
} 
public class Requests 
{ 
    public string jsonrpc { get; set; } 
    public string method { get; set; } 
    public List<string> param { get; set; } 
    public string id { get; set; } 
} 

任何想法?

+0

您的屬性稱爲'param'不'params'。我想這就是它的原因 –

回答

3

由於錯誤的拼寫錯誤的,應該是:

ParamS // Add one more S 

所以,你的類應該是:

public class Requests 
{ 
    public string jsonrpc { get; set; } 
    public string method { get; set; } 
    public List<string> @params { get; set; } 
    public string id { get; set; } 
} 
+0

這樣做。謝謝! – Sugitime

+0

只是在這裏嚴重...但我不會建議使用保留關鍵字作爲屬性名稱。相反,可能會更好地重命名爲'Parameters'。 – James

相關問題