Reputation: 53
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": "[email protected]",
"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": "[email protected]",
"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.
Upvotes: 0
Views: 395
Reputation: 5901
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": "[email protected]",
"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": "[email protected]",
"events": [
{
"id": 2,
"name": "Event 2",
"date": "17/10/2019",
"finish": 0
}
]
}
]
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
.
Upvotes: 4
Reputation: 388
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.
Upvotes: 0