Dillinger
Dillinger

Reputation: 1903

How to create an array of object?

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?

Upvotes: 0

Views: 104

Answers (2)

Amaterasu
Amaterasu

Reputation: 11

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

Upvotes: 1

Wicher Visser
Wicher Visser

Reputation: 1543

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#.

Upvotes: 3

Related Questions