4

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?

7
  • 4
    searchUser returns a List of Tenants, but you're trying to return a single Tenant from it. Simply create a list of Tenants and select the relevant Tenants from the JSON object and return those as a list. Or if it's really User objects you require then return a List<User> instead. You can either add them directly to the List or use LINQ. Commented Apr 22, 2016 at 15:00
  • 1
    @ManoDestra uhm I think that you mean var obj = JsonConvert.DeserializeObject<List<RootObject>>("json"); right? If yes, I already tried, and I got: obj does not contain any definition for tenant Commented Apr 22, 2016 at 15:03
  • Your singular RootObject at the moment has a singular Tenant object within it. If you wish to return Tenant, then you'll have to change the method signature to return Tenant, rather than List<Tenant>. If you wish users, then return List<User>. If it's the full list you require, then simply return obj.tenant.user; (Your property Names should be upper case BTW) :) Commented Apr 22, 2016 at 15:06
  • 1
    @IlDrugo: can you post a sample JSON file so that we can debug it? Commented Apr 22, 2016 at 15:08
  • Just change this line: public List<Tenant> searchUser() to this line public Tenant searchUser() Commented Apr 22, 2016 at 15:10

2 Answers 2

3

Depends on which you really require.

If you require the Tenant object returned, then change your method signature/return to this...

public Tenant getTenant()
{
    var obj = JsonConvert.DeserializeObject<RootObject>("json");
    return obj.tenant;
}

If you require the users instead, change your method signature/return to this...

public List<User> getUsers()
{
    var obj = JsonConvert.DeserializeObject<RootObject>("json");
    return obj.tenant.user; //?
}

However, I would recommend changing your property names to have the correct upper casing. And if you're returning a list of User objects, then it would make sense for that property to be called Users, instead of user. Think about your data model, whether each link is one to one or one to many, etc, and code/name accordingly.

MS naming recommendations (https://msdn.microsoft.com/en-us/library/ms229012.aspx).

Sign up to request clarification or add additional context in comments.

2 Comments

you mean the class property? I read on a c# book that is a good choice use the CamelCase
2

The problem is that your method signature suggests that you will return a List<Tenant> whereas your return type clearly says it is a single Tenant:

public List<Tenant> searchUser() {
    var obj = JsonConvert.DeserializeObject<RootObject>("json");
    return obj.tenant; //Type is Tenant (according to RootObject)
}

One of the two (or both) are false. If one is false, there are two options:

  1. So you either should alter the return type to Tenant:

    public Tenant searchUser() {
        var obj = JsonConvert.DeserializeObject<RootObject>("json");
        return obj.tenant; //Type is Tenant (according to RootObject)
    }
    
  2. otherwise you have to alter the RootObject type for tenant:

    public class RootObject
    {
         public List<Tenant> tenant { get; set; }
    }
    

Based on your JSON file, it is the first option (1.).

You can then access the User and Detail by using the result of the searchUser: something like:

var tenant = searchUser();
var user = tenant.user;
var detail = tenant.detail;

//... do something with user and detail

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.