Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 5011

Deserializing SFDC REST services JSON response

Receiving following response from SFDC REST Web Service (string jsonResponse below):

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

Trying to deserialize it with ServiceStack.JsonSerializer:

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

ResponseDataList has following structure:

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; }
}

However after deserialization ResponseDataList list is null. What do I do wrong, how to deserialize correctly?

Upvotes: 1

Views: 695

Answers (2)

Jedediah
Jedediah

Reputation: 1944

If you're open to using Json.NET to do the deserialization instead of Service Stack, you can easily do it as follows without having to make the DataResponseList class:

var client = new WebClient();
var json = client.DownloadString("url");
IList<ResponseData> data = JsonConvert.DeserializeObject<List<ResponseData>>(json);

Upvotes: 1

Patrick Hallisey
Patrick Hallisey

Reputation: 943

By making responseDataList public, I was able to deserialize properly.

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

Upvotes: 4

Related Questions