1

从 SFDC REST Web 服务接收以下响应(下面的字符串jsonResponse):

{
    "responseDataList": [{
        "salesforceRecordId": "a00C000000L5DQRIA3",
        "recordName": "Computer_Name_2",
        "IsSuccess": true,
        "errorMessage": null
    }]
}

尝试使用 ServiceStack.JsonSerializer 对其进行反序列化:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string jsonResponse = new StreamReader(response.GetResponseStream()).ReadToEnd();
    ResponseDataList list = JsonSerializer.DeserializeFromString<ResponseDataList>(jsonResponse);

ResponseDataList 具有以下结构:

public class ResponseData
{
    public string salesforceRecordId { get; set; }
    public string recordName { get; set; }
    public bool IsSuccess { get; set; }
    public string errorMessage { get; set; }
}

public class ResponseDataList 
{
    List<ResponseData> responseDataList { get; set; }
}

但是在反序列化之后 ResponseDataList 列表为空。我做错了什么,如何正确反序列化?

4

2 回答 2

4

通过公开 responseDataList,我能够正确反序列化。

public class ResponseDataList 
{
    public List<ResponseData> responseDataList { get; set; }
}
于 2013-06-18T20:49:30.130 回答
1

如果您愿意使用 Json.NET 而不是 Service Stack 进行反序列化,您可以轻松地按照以下方式进行操作,而无需创建DataResponseList类:

var client = new WebClient();
var json = client.DownloadString("url");
IList<ResponseData> data = JsonConvert.DeserializeObject<List<ResponseData>>(json);
于 2013-06-18T20:52:10.827 回答