Jamie
Jamie

Reputation: 10896

Parse Json to grid

I'm trying to serialize this json:

{
  "tickets": [
    {
      "id": 1,
      "title": "Probleem met windows",
      "description": "Kan geen programma installeren.",
      "user_id": 1,
      "subject_id": 1,
      "status_id": 1,
      "status": {
        "id": 1,
        "name": "Afgerond"
      },
      "subject": {
        "id": 1,
        "subject": "Hardware"
      },
      "user": {
        "id": 1,
        "name": "test",
        "email": "[email protected]",
        "company": "test",
        "role_id": 2,
        "created_at": null,
        "updated_at": "2016-09-16 20:45:30"
      }
    }
  ]
}

And show it within a datagrid like this:

  private void getTickets()
        {
            try
            {
                string urlName = "tickets";
                string method = "GET";
                string json = null;
                HttpStatusCode statusCode = HttpStatusCode.Forbidden;
                Ticket data = null;

                RestClient client = RequestClient.makeClient();
                MakeRequest request = new MakeRequest(null, null, urlName, method);

                IRestResponse response = client.Execute(request.exec());

                statusCode = response.StatusCode;
                json = response.Content;
                data = JsonConvert.DeserializeObject<Ticket>(json);

                if ((int)statusCode == 200)
                {
                    gridTickets.DataSource = data;
                }

                else
                {
                    MessageBox.Show("error");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

So if I debug there is a correct response. But the data is not loaded correctly in the grid. What could be wrong here?

enter image description here

--EDIT--

Ticket:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicketSystem
{
    class Ticket
    {
        public int id;
        public string title;
        public string description;
        public int user_id;
        public int subject_id;
    }
}

Upvotes: 0

Views: 746

Answers (1)

Quentin Roger
Quentin Roger

Reputation: 6538

Another solution can be :

   var json = "{\r\n  \"tickets\": [\r\n    {\r\n      \"id\": 1,\r\n      \"title\": \"Probleem met windows\",\r\n      \"description\": \"Kan geen programma installeren.\",\r\n      \"user_id\": 1,\r\n      \"subject_id\": 1,\r\n      \"status_id\": 1,\r\n      \"status\": {\r\n        \"id\": 1,\r\n        \"name\": \"Afgerond\"\r\n      },\r\n      \"subject\": {\r\n        \"id\": 1,\r\n        \"subject\": \"Hardware\"\r\n      },\r\n      \"user\": {\r\n        \"id\": 1,\r\n        \"name\": \"test\",\r\n        \"email\": \"[email protected]\",\r\n        \"company\": \"test\",\r\n        \"role_id\": 2,\r\n        \"created_at\": null,\r\n        \"updated_at\": \"2016-09-16 20:45:30\"\r\n      }\r\n    }\r\n  ]\r\n}";
   var data = JsonConvert.DeserializeObject<dynamic>(json);
   DataGrid.ItemsSource = data.tickets;

Upvotes: 1

Related Questions