Reputation: 971
I am using IPWorks nsoftware for creating service. In it, to call a service I am using
Rest rest = new Rest();
rest.Accept = "application/json";
rest.ContentType = "application/json";
rest.User = "UserName";
rest.Password = "Password";
rest.Get(@"http://Foo.com/roles.json");
string result = rest.TransferredData;
var listRoles = JsonSerializer.DeserializeFromString<List<role>>(result);
I am getting the Json response as a string
[{"role":{"name":"Administrator","created_at":"2012-02-11T09:53:54-02:00","updated_at":"2012-04-29T23:43:47-04:00","id":1"}},{"role":{"name":"NormalUser","created_at":"2013-02-11T08:53:54-02:00","updated_at":"2013-04-29T23:43:47-03:00","id":2"}}]
Here the json string contains my domain object “role” which gets appended to my response (i.e the body style of the message is wrapped) . I am using ServiceStack.Text’s Deserializer to convert the response string to my object. But since it’s wrapped, the deserilization is incorrect.
Is there anything that I am missing here ? Is there any “BodyStyle” attribute which could be added to the Rest request?
Upvotes: 2
Views: 468
Reputation: 143284
The GitHubRestTests shows some of the different ways you can deserialize a 3rd party json API with ServiceStack's JSON Serializer.
If you want to deserialize it into typed POCOs then judging by your JSON payload the typed POCOs should look something like:
public class RolePermissionWrapper
{
public Role Role { get; set; }
public Permission Permission { get; set; }
}
public class Role
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime? Created_At { get; set; }
public DateTime? Updated_At { get; set; }
}
var listRoles = JsonSerializer.DeserializeFromString<List<RolePermissionWrapper>>(result);
Upvotes: 2