2014-03-19 43 views
-2

這是一個我想將它映射到C#poco類的json文檔。我寫了一些課,但他們沒有工作。我的結果對象中有空。有任何想法嗎?Json在C中沒有正確映射#

我使用了Newtonsoft的json轉換器。

{ 
    "retrieval-response":{ 
    "cdata":{ 
    "identifier":"777400", 
    "document-count":"62" 
    }, 
    "index":"10", 
    "count":"25" 
    } 
} 

C#map classes;

public class result 
{ 
    [JsonProperty("retrieval-response")] 
    public aResult res { get; set; } 
    public int index { get; set; } 
    public int count { get; set; } 

} 

public class aResult 
{ 
    public cdata data { get; set; } 
} 

public class cdata 
{ 
    [JsonProperty("identifier")] 
    public string identif { get; set; } 
    [JsonProperty("document-count")] 
    public string count { get; set; } 
} 
+2

映射?映射如何? – Liam

回答

2

您的模型是錯誤的。試試這個:

public class Wrapper 
{ 
    [JsonProperty("retrieval-response")] 
    public Result Result { get; set; } 
} 

public class Result 
{ 
    [JsonProperty("cdata")] 
    public Data Data { get; set; } 

    public int Index { get; set; } 

    public int Count { get; set; } 
} 

public class Data 
{ 
    [JsonProperty("identifier")] 
    public string Identifier { get; set; } 

    [JsonProperty("document-count")] 
    public string Count { get; set; } 
} 

然後你可以用下面這行反序列化:

var myResult = JsonConvert.DeserializeObject<Wrapper>(json); 

請注意,我也帕斯卡的情況下寫了你的財產和類名。這些是naming conventions from Microsoft