Deserialize JSON with Newtonsoft - c#

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.

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.

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.

Related

Collection Navigation Properties returning odd JSON hierarchy

I have 2 classes:
public class A
{
public int Id { get; set; }
public string Name { get; set; }
public B myB { get; set; }
}
public class B
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<A> myAs { get; set; }
}
Im using Postman to test the Api calls.
public IEnumerable<B> GetBs()
{
return _context.Bs.Include(b => b.myAs).ToList();
}
returns as expected, B objects and a list of their associated A objects:
{
"Id": 1,
"Name": "B2",
"myAs": [
{
"Id": 1,
"Name": "A1"
},
{
"Id": 2,
"Name": "A2"
}
]
},
{
"Id": 2,
"Name": "B3",
"myAs": [
{
"Id": 3,
"Name": "A3"
},
{
"Id": 4,
"Name": "A4"
},
{
"Id": 5,
"Name": "A5"
}
]
}
The reverse however returns a wierd hierarchical structure:
public IEnumerable<A> GetAs()
{
return _context.As.Include(a => a.myB).ToList();
}
returns:
[
{
"Id": 1,
"Name": "A1",
"myB": {
"Id": 1,
"Name": "B2",
"myAs": [
{
"Id": 2,
"Name": "A2"
}
]
}
},
{
"Id": 2,
"Name": "A2",
"myB": {
"Id": 1,
"Name": "B2",
"myAs": [
{
"Id": 1,
"Name": "A1"
}
]
}
},
{
"Id": 3,
"Name": "A3",
"myB": {
"Id": 2,
"Name": "B3",
"myAs": [
{
"Id": 4,
"Name": "A4"
},
{
"Id": 5,
"Name": "A5"
}
]
}
}
]
The GetAs method returns A objects with B objects with further nested A objects.
My understanding after a little research (I could be very wrong here), is that because A has a navigation property to B (myB) and B has a navigation property to a list of A objects (myAs) this is causing some kind of loop.
My questions are
Is my understanding here correct ? Is this why the hierarchy is returning in this weird layout ?
How do i fix this ? I can take the ICollection navigation property out of the domain model but then I can no longer query As and their associated Bs ?
note A and B are not actually my domain models. I just wanted to keep the example as simple as possible.
Thanks in advance.
A few things here:
The output has the expected shape. As you suspect, the bidirectional references are being expanded by the serializer. Think of what would happen if you manually serialized each property of each object recursively. That is what happening.
To solve the immediate problem configure you default serializer settings like this:
jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.Serialization.ReferenceLoopHandling.Ignore;
The above is useful when prototyping but when your application is more formalized, you should create and return dedicated view model types from your web API endpoints.
public class AViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class BViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<AViewModel> myAs { get; set; }
}
public IEnumerable<BViewModel> GetBs()
{
return _context.Bs.Include(b => b.myAs)
.Select(b => new BViewModel
{
Id = b.Id,
Name = b.Name,
As = b.As.Select(a => new AViewModel
{
Id = a.Id,
Name = a.Name
})
})
.ToList();
}
It is worth noting that there are libraries, such as the well regarded AutoMapper, that can perform these translations between model types for you, automatically assigning corresponding properties by name using reflection.
Personally, I try to avoid reflection based approaches as much as possible as they tend to render code difficult to reason about statically. This hinders both human readers such as ourselves and tools like the C# language.
That said, it can be worth the tradeoff depending on the task at hand. I hope to eventually see language level support that eliminates such boilerplate assignments without stepping into the realm of stringiness but I have a long time to wait.

Cannot convert json to object

Hello i've got this error Message
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ClassLibraryMifosX.ViewModels.Rootobject2' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JS
my deserialize code :
Rootobject Rsc = JsonConvert.DeserializeObject<Rootobject>(json);
my class with json object description:
public class Rootobject
{
public List<Class1> Property1 { get; set; }
}
public class Class1
{
public int entityId { get; set; }
public string entityAccountNo { get; set; }
public string entityExternalId { get; set; }
public string entityName { get; set; }
public string entityType { get; set; }
public int parentId { get; set; }
public string parentName { get; set; }
public string entityMobileNo { get; set; }
public Entitystatus entityStatus { get; set; }
}
public class Entitystatus
{
public int id { get; set; }
public string code { get; set; }
public string value { get; set; }
}
my json :
[
{
"entityId": 1,
"entityAccountNo": "000000001",
"entityExternalId": "100001-241563",
"entityName": "Smith W R",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityMobileNo": "254728000000",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 310,
"entityAccountNo": "000000310",
"entityName": "John Smith",
"entityType": "CLIENT",
"parentId": 14,
"parentName": "TestOffice1",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 422,
"entityAccountNo": "000000422",
"entityExternalId": "smith1",
"entityName": "Smith Jones",
"entityType": "CLIENT",
"parentId": 11,
"parentName": "Barquisimeto",
"entityMobileNo": "88989898",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 774,
"entityAccountNo": "000000774",
"entityName": "John AAA Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 1789,
"entityAccountNo": "Head Office000001789",
"entityExternalId": "547222",
"entityName": "Kaitlin Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
}
]
what i have been done wrongly ? Thanks
There is no root object into your Json data so just deserialize it as a collection of Class1 like below:
var collection = JsonConvert.DeserializeObject<List<Class1>>(json);
Don't forget that VS can create for you a class that can be used to deserailize your Json data. You don't need to write yourself the definition of Class1. Just go to menu => Edit > Paste Special > Paste JSON as classes
The first and last character of your JSON is a square bracket [ ] rather than a curly bracket { }. This means that it is an array, not an object. In order to parse it, you need to deserialize it into an array of Class1 objects:
Class1[] Rsc = JsonConvert.DeserializeObject<Class1[]>(json);
If you wanted to use a Rootobject object instead, you could then use it like so:
Rootobject root = new RootObject();
root.Property1 = new List<Class1>(Rsc);

Deserialize nested JSON array in C#

I have a JSON array with nested objects, representing a menu, as this:
[
[
{
"name": "Item 1",
"id": 1
},
{
"name": "Item 2",
"id": 2,
"children": [
[
{
"name": "Item 21",
"id": 21
}
]
]
},
{
"name": "Item 3",
"id": 3,
"children": [
[
{
"name": "Item 31",
"id": 31,
"children": [
[
{
"name": "Item 311",
"id": 311
},
{
"name": "Item 312",
"id": 312
}
]
]
},
{
"name": "Item 32",
"id": 32
},
...
And I want to deserialize it using JavaScriptSerializer. I have some code as shown below but is not working.
var serializer = new JavaScriptSerializer();
var objects = serializer.Deserialize<Menu>(jsonData);
...
public class Menu
{
public int id { get; set; }
public string name { get; set; }
public Menu[] children { get; set; }
}
The error I get is "The type 'Menu' is not supported to deserialize a matrix".
I would appreciate any help on how to declare the custom object.
Cheers.
Your root object is a 2d jagged array of objects. The properties "children" are also 2d jagged arrays. Thus your Menu class needs to be:
public class Menu
{
public int id { get; set; }
public string name { get; set; }
public Menu [][] children { get; set; }
}
And deserialize your JSON as follows:
var serializer = new JavaScriptSerializer();
var objects = serializer.Deserialize<Menu [][]>(jsonData);
Alternatively, if you prefer lists to arrays, do:
public class Menu
{
public int id { get; set; }
public string name { get; set; }
public List<List<Menu>> children { get; set; }
}
And then
var objects = serializer.Deserialize<List<List<Menu>>>(jsonData);
Could the issue be that the actual data is an array but you're telling it to expect just one Menu?

How to deserialize JSON objects to List<> when the items are not in a Json array? (C#)

I'm using a Json string from another system. It looks something like this:
{
"BoolValue": true,
"Inventory": {
"Item1": {
"id": "1",
"name": "Item One"
},
"Item2": {
"id": "2",
"name": "Item Two"
},
"Item3": {
"id": "2",
"name": "Item Three"
}
}
}
How would I deserialize the "Item" objects to a List?
I know it's easy then the json uses an array for "Inventory": [] but how will I do it when it's just object after object under the Inventory property?
If I'm understanding correctly, you'll need a class setup like this:
public class Results {
public bool BoolValue { get; set; }
public Dictionary<string, Item> Inventory { get; set; }
}
public class Item {
public string id { get; set; }
public string name { get; set; }
}

How to query data from json in c# windows phone

I am new to windows phone dev. I'm working on an app that fetch json from a web service and parse it and display it to the app. I used json.net to parse it. here's my json file:
[
{
"id": "001",
"title": "title1",
"content": "sample content",
"category_id": "3",
"image": "defaultimg.jpg"
},
{
"id": "021",
"title": "title2",
"content": "sample content",
"category_id": "1",
"image": "defaultimg2.jpg"
},
{
"id": "011",
"title": "title3",
"content": "sample content",
"category_id": "3",
"image": "defaultimg22.jpg"
},
{
"id": "008",
"title": "title24",
"content": "sample content",
"category_id": "2",
"image": "defaultimg12.jpg"
},
{
"id": "121",
"title": "title12",
"content": "sample content",
"category_id": "3",
"image": "defaultimg27.jpg"
}
]
so I came up with this class with the help of json2csharp.com
public class RootObject
{
public string id { get; set; }
public string title { get; set; }
public string content { get; set; }
public string category_id { get; set; }
public string image { get; set; }
}
here's my code in cs
var data = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(data, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);
ListBox1.ItemsSource = deserialized;
});
data.DownloadStringAsync(
new Uri("http://sampleurl.com/xyz/myjson.aspx"));
I want to display only those who has "category_id": "9" on the listbox1
can you help me how to filter this data? im a student and new in c# windows phone. Thanks!
You would generally want to use LINQ to manipulate your List<RootObject>, something like:
var deserialized = JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);
// select only RootObjects with category_id equal to 9
ListBox1.ItemsSource = deserialized.Where(r => r.category_id == 9);
If the API itself does not have an endpoint that allows you to query for only those records with category_id : 9 then you'll have to do the filtering at the client to populate your listbox. One common and easy way to do that is with LINQ.
Here is an example of what the LINQ syntax would look like:
var categoryNineOnly = data.Where(x=>x.category_id == 9)
Much more detail here:
http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx

Categories

Resources