C#gridView在json中查看多个对象

时间:2016-09-29 11:18:39

标签: c# json gridview

我在网格中显示成功的数据:

像这样:

形式:

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

TicketWrapper:

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

门票:

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

所以我的问题:

如何显示name的{​​{1}}。我已经尝试过这样的事情:

user

1 个答案:

答案 0 :(得分:1)

您的Ticket课程不完整。

将Json粘贴到http://json2csharp.com会返回此类层次结构(编辑以删除多余数据)

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

对此进行反序列化后,可以通过ticket.User.name

访问用户名