I need to deserialize a json, so I have created this class structure:
public class Detail
{
public string code { get; set; }
public string type { get; set; }
}
public class User
{
public string name { get; set; }
public string surname { get; set; }
public string lastname { get; set; }
}
public class Tenant
{
public List<User> user { get; set; }
public List<Detail> detail { get; set; }
}
public class RootObject
{
public Tenant tenant { get; set; }
}
Now I need to return an object that contain User and Detail. In particular I want send back to the user an object that allow to access to obj.Tenant.User and obj.Tenant.Detail. This is my method that send back the data:
public List<Tenant> searchUser()
{
var obj = JsonConvert.DeserializeObject<RootObject>("json");
return obj.tenant; //?
}
the Tenant object contains both class user and detail. Now user is a list of users, and detail contains a list of details for each user. But I unfortunately got this error message:
Can not be converted implicitly the 'User.Tenant' type 'System.Collections.Generic.List <User.Tenant>' Tenant
on this line: return obj.tenant;
I guess that I wrong the type that send back the data. But how can I fix this?
var obj = JsonConvert.DeserializeObject<List<RootObject>>("json");right? If yes, I already tried, and I got:obj does not contain any definition for tenantreturn obj.tenant.user;(Your property Names should be upper case BTW) :)public List<Tenant> searchUser()to this linepublic Tenant searchUser()