0

I'm having problems with this JSON to use it in combobox and datagridview. I'm using Newtonsoft to do it:

{
    "users": [
        {
            "id": 1,
            "name": "Test 1",
            "email": "test1@test.com",
            "events": [
                {
                    "id": 1,
                    "name": "Event 1",
                    "date": "11/10/2019",
                    "finish": 0

                },
                {
                    "id": 2,
                    "name": "Event 2",
                    "date": "12/10/2019",
                    "finish": 0
                }
            ]
        },
        {
            "id": 2,
            "name": "Test 2",
            "email": "test2@test.com",
            "events": [
                {
                    "id": 2,
                    "name": "Event 2",
                    "date": "17/10/2019",
                    "finish": 0
                }
            ]
        }
    ]
}

And this is the class (JsonEvent.cs). Generated with json2csharp.com:

using System;
using System.Collections.Generic;

namespace TestDO.Models
{
    class JsonEvent
    {
        public partial class Admin
        {
            public List<User> Users { get; set; }
        }

        public partial class User
        {
            public long Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public List<Event> Events { get; set; }
        }

        public partial class Event
        {
            public long Id { get; set; }
            public string Name { get; set; }
            public string Date { get; set; }
            public long Finish { get; set; }
        }
    }
}

But now, I do not know how to use the result with combobox datasource or a datagrid.

So far the only time I've worked with this has been for a much simpler topic like this (this is and example, It is not related to this): JsonResponde.cs

namespace TestDO.Models
{
    class JsonResponse
    {
        public string Upgraded { get; set; }
        public string time{ get; set; }
    }
}

And then I check the result of Upgraded like this:

var jResponse = JsonConvert.DeserializeObject<JsonResponse>(json);
if (jResponse.Upgraded == "true")

But I don't know how to do it with a more complex json.

I want to use event id for combobox and event name for display.

And for datagrid, user id, user name, event name, event date for each line.

Thank you in advance for any help to solve the problem.

Mendizalea
  • 53
  • 8
  • 4
    What about `JsonConvert.DeserializeObject(json);`? – Twenty Dec 19 '19 at 20:45
  • 1
    @Twenty is spot on, then your `jResponse` will have `Users` filled, that you can then use. Also `Upgraded` doesn't exist in `Admin` you will have an exception there. – Trevor Dec 19 '19 at 20:48
  • 1
    Do you need help deserializing the data, or do you want to know how to use your class as a ComboBox or DataGrid datasource? – haldo Dec 19 '19 at 20:55
  • Does this answer your question? [How do I deserialize a complex JSON object in C# .NET?](https://stackoverflow.com/questions/16339167/how-do-i-deserialize-a-complex-json-object-in-c-sharp-net) – Heretic Monkey Dec 19 '19 at 20:56
  • @Çöđěxěŕ I think the OP just copied the code from another source which isn't directly correlating with the question itself. – Twenty Dec 19 '19 at 21:02
  • @Twenty possibility, what here gives you that assumption? – Trevor Dec 19 '19 at 21:04
  • @haldo I want to know to use my class with a combobox or datagrid. – Mendizalea Dec 19 '19 at 21:07
  • 1
    @Çöđěxěŕ > `So far the only time I've worked with this has been for a much simpler topic like this`. I mean yea it is not a 100% guarantee, but most likely. – Twenty Dec 19 '19 at 21:07
  • 1
    @Twenty good point, not sure; it's definitely a possibility. – Trevor Dec 19 '19 at 21:09
  • Please update your question to make it clear that you want to use the class, not deserialize the data. Which fields do you want to show in the combobox? How will you handle the list of `Event`s? This is tagged as JSON but its not actually about deserializing json... – haldo Dec 19 '19 at 21:12
  • @Mendizalea Take a look at my answer, I updated it to your needs. Of course it is important which fields you want to show as haldo mentioned, in order to provide a proper solution. – Twenty Dec 19 '19 at 21:17
  • @haldo I updated the question. – Mendizalea Dec 19 '19 at 21:45
  • @Twenty, I will see it now, thanks ;) – Mendizalea Dec 19 '19 at 21:45
  • @Mendizalea I added a possible solution for the combo box as well. – Twenty Dec 19 '19 at 21:52

2 Answers2

4

Your C# representation of the JSON you provided seems to perfectly fine and matching. Therefor you should be able to do something like the following:

var jResponse = JsonConvert.DeserializeObject<Admin>(json);

If the JSON array Users is not nested in another object you could also convert it to a List<User> (HashSet or similar collections should also work), if it's the only property of your Admin class. This would look something like this:

var jResponse = JsonConvert.DeserializeObject<List<User>>(json);

For this to work your JSON would need to look something like this:

[
    {
        "id": 1,
        "name": "Test 1",
        "email": "test1@test.com",
        "events": [
            {
                "id": 1,
                "name": "Event 1",
                "date": "11/10/2019",
                "finish": 0

            },
            {
                "id": 2,
                "name": "Event 2",
                "date": "12/10/2019",
                "finish": 0
            }
        ]
    },
    {
        "id": 2,
        "name": "Test 2",
        "email": "test2@test.com",
        "events": [
            {
                "id": 2,
                "name": "Event 2",
                "date": "17/10/2019",
                "finish": 0
            }
        ]
    }
]

Edit

If you want to feed your users to a combo-box, you can just pass the list to the combo-box. See the example below:

ComboBox cb = new ComboBox();

cb.DataSource = jResponse.Users.SelectMany(x => x.Events.OrderBy(y => y.Id).Select(y => y.Name));

Of course your Users object contains a lot of information, that is just an assumption, that you want to have all the names of the users in the ComboBox.

Twenty
  • 5,234
  • 4
  • 32
  • 67
  • Thanks @Twenty. Finally look like this: `CboEvents.DataSource = jResponse.Users.SelectMany(x => x.Events.OrderBy(y => y.Id).Select(y => new { y.Id, y.Name })).ToList();` Linq it's new to me and I don't quite understand the x and the y are but it works. – Mendizalea Dec 20 '19 at 06:43
  • I change deserialize to because only didn't work and SelectMany result get a List – Mendizalea Dec 20 '19 at 06:50
0

You need to deserialize your json to an object of the Admin class you created, which you can do like this:

var AdminObj = JsonConvert.DeserializeObject<Admin>(json);

From there, you have access your Users list by AdminObj.Users, which you can loop through to get whatever data you need for your application.

ShaneDemskie
  • 389
  • 2
  • 14