Jamie Jamie - 2 months ago 13
JSON Question

C# gridView multiple objects in json

I'm showing successful data within my grid:

Like this:

Form:

json = response.Content;
var ticketWrapper = JsonConvert.DeserializeObject<TicketWrapper(json);


TicketWrapper:

class TicketWrapper
{
public IEnumerable<Tickets> tickets { get; set; }
}


Tickets:

class Tickets
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
}


Json:

{
"tickets": [
{
"id": 1,
"title": "Error bij compileren.",
"description": "Bij het compileren van mijn c# applicatie krijg ik een error. ",
"user_id": 1,
"subject_id": 1,
"status_id": 1,
"status": {
"id": 1,
"name": "In afwachting"
},
"subject": {
"id": 1,
"subject": "C#"
},
"user": {
"id": 1,
"name": "test",
"email": "test@gmail.com",
"company": "production",
"role_id": 1,
"created_at": null,
"updated_at": "2016-09-08 08:22:07"
}
}
]
}


So my question:

How would I show the
name
of a
user
. I already tried something like this:

class Tickets
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
}

Answer

Your Ticket class is incomplete.

Pasting your Json into http://json2csharp.com returns this class hierarchy (edited to remove superfluous data)

public class Ticket
{
    public int id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public Status status { get; set; }
    public Subject subject { get; set; }
    public User user { get; set; }
}

public class RootObject
{
    public List<Ticket> tickets { get; set; }
}

public class Status
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Subject
{
    public int id { get; set; }
    public string subject { get; set; }
}

public class User
{
    public int id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string company { get; set; }
    public int role_id { get; set; }
    public object created_at { get; set; }
    public string updated_at { get; set; }
}

Once you have deserialized to this, the user name would be accessible via ticket.User.name

Comments