0

I've created a dll that perform some request on my API. Now the request send me back a json response, what I want to do (from the dll), is return an array of object to be used in the program that implements the DLL. Now I've this class:

public class Details
{
    public string name { get; set; }
    public string age { get; set; }
}

public class Info
{
    public List<object> info { get; set; }
}

public class User
{
    public Details details { get; set; }
    public Info info { get; set; }
}

public class RootObject
{
    public User user { get; set; }
}

I deserialize the request like this:

var obj = JsonConvert.DeserializeObject<List<RootObject>>("json returned");

Now the json contains the details of the user, and in some case also the info, so I iterate through of it in this way:

foreach(var user in obj)
{
    item.user.details.name;
    //take some info (could not contain nothing)
    foreach(var info in user.info.info)
    {
         info; //<- contains all the information
    }
}

What I want to know is: How can I create a list of object? In particular I want send back the user object that have as property details and info. The result should be an array of object 'cause who reiceve the object need to iterate through of it and read each object property as:

user[0].details.name: //where 0 is the index of the user (php syntax)

I don't know if is possible in c#, someone could help me to achieve this target?

1
  • 1
    You want a list of all user objects? Try this: var users = obj.Select(x => x.user).ToArray(); Commented Apr 16, 2016 at 9:31

2 Answers 2

3

Your json converter returns List<RootObject>, and each RootObject contains only one property: user. A simple Linq query would change the List<RootObject> into a List<User> object:

var users = obj.Select(o => o.user).ToList();

Each element in users then is a User, with both the Details and Info property.

As an example on how to use this, consider you have a method that does the conversion from json and you want that method to return the list of users. That method would look something like this:

public List<User> GetUsersFromJson()
{
    var obj = JsonConvert.DeserializeObject<List<RootObject>>("json returned");
    var users = obj.Select(o => o.user).ToList();
    return users;
}

You can iterate through the users object like this:

foreach (var user in users)
{
  var detail = user.details;
  var info = user.info;
}

You should consider changing your public properties to camel-case as is common practice in C#.

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

6 Comments

should I declare the method as public List<T>?
the Linq query is not a method in itself. You can add it anywhere in your code to do the conversion. The method into which you embed it can be public, private or whatever depending on your design.
Ok seems working fine, just one thing I save the object like this: object user = User.GetUsersFromJson(); but I don't understand how can I iterate through it, 'cause if I do: user. the intellisense doesn't show any User class property, instead if I put the break point I can see the list of user and the property.
I get this error: The instruction foreach can't working with variable of object type 'cause object does not contain a public definition for GetEnumerator. In particular users is underlined in red.. what is wrong?
Without knowing your code it is not possible to tell exactly what you did wrong. Most likely the object you use in the foreach is not a collection.
|
1

JsonConvert.DeserializeObject> returns the list of RootObject. You can use method ToArray() to change the list to array.

In this case:

var obj = JsonConvert.DeserializeObject<List<RootObject>>("json returned");
RootObject[] array = obj.ToArray();

string s = array[0].user.details.name;
object[] infos = array[0].user.info.info.ToArray();

and in this code:

foreach(var user in obj)
{
    item.user.details.name;
    //take some info (could not contain nothing)
    foreach(var info in user.info.info)
    {
         info; //<- contains all the information
    }
}

don't have sens, it should be like this:

foreach (RootObject elem in obj)
{
     foreach (Info info in elem.user.info.info)
     {
         object[] localInfo = info.info.ToArray(); //<- contains all the information
     }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.