Richard West
Richard West

Reputation: 2226

Setting up a class to contain a list of objects

I am setting a class in C# to hold a response that I will be receiving from a web service call. I'm using RestSharp to handle the calling / parsing of the JSON data.

The web service documentation describes the response as:

OUTPUT: JSON object similar to example below.
    {
        "response":"[success] or [failed]",
        "messages":"Process succeeded.",
        "logonkey":"[logon key]",
        "tokenkey":"[security token]",
        "reccount":"1",
        "filelist":
            {
            "fileid":"12345",
            "status":"N",
            "filename":"data.tar",
            "fulfilled":"2012-06-15"
        }
    }

My question is how can I define the filelist array element in my class?

I was thinking something like this:

public class Files
{
    public string Response { get; set; }
    public string Messages { get; set; }
    public string LogonKey { get; set; }
    public string TokenKey { get; set; }
    public int RecordCount { get; set; }
    public List<FileList>
}

public class FileList
{
    public string FileID { get; set; }
    public string Status { get; set; }
    public string Filename { get; set; }
    public DateTime Fulfilled { get; set; }
}

However, I'm having a problem with the "public List" statement.

Any suggestions on the best way to handle this scenario would be appreciated.

Upvotes: 1

Views: 2245

Answers (2)

Freeman
Freeman

Reputation: 5801

Also you forgot to give it a name, for example try this

public List<FileList> MyFavouriteList {get;set;}. 

A proprety is declared like this "access modifier", "Type", "identifier(or name)", "getter and setter".

Upvotes: 2

vetti
vetti

Reputation: 309

The filelist in JSON sample doesn't look like list of objects. Instead it is a single object. You could try,

public class Files
{
    public string Response { get; set; }
    public string Messages { get; set; }
    public string LogonKey { get; set; }
    public string TokenKey { get; set; }
    public int RecordCount { get; set; }
    public FileList File   {get; set; }
}

If you are sure the filelist in JSON is really list of object, you could try,

public List<FileList> Files   = new List<FileList>();

Upvotes: 1

Related Questions