2012-09-05 49 views
1

我碰到下面的JSON,我想转换到使用RestSharpRestSharp:转换结果

{ 
    "valid":true, 
    "data":[ 
    { 
     "dealerId":"4373", 
     "branchId":"4373", 
    } 
    ] 
    } 

我想转换为业务对象:

public class Dealer 
{ 
    public string dealerId ; 
    public string branchId; 
    } 

但是失败了,虽然JSON很好:

 var client = new RestClient("http://www.????.com.au"); 
     var request = new RestRequest(string.Format("service/autocomplete/dealer/{0}/{1}.json", suburb.PostCode, suburb.City.Trim().Replace(" ", "%20")), Method.GET); 
     var response2 = client.Execute<Dealer>(request); 
     return response2.Data; 

回答

1

您的业务对象与您回复的响应JSON不匹配。如果你希望你的反应序列化,你的C#的对象会看起来像

public class DealerResponse 
{ 
    public bool valid { get;set; } 
    List<Dealer> data { get;set; } 
} 

public class Dealer 
{ 
    public string dealerId;   
    public string branchId; 
} 

我没有测试此代码,但即使你是只在“数据”的信息感兴趣,你的反应C#对象仍需要表示整个JSON响应才能正确序列化。

希望有所帮助。

+0

非常好。我知道必须有一个简单的方法来做到这一点。奇迹般有效。投票++ –