C# Parse nested JSON Array to get values - c#

I've been searching and trying to get this to work for hours and i'm completely out of ideas. I have JSON text that i'm trying to read and can't see it get it to work. Here is the JSON text.
[ {
"first_aired": "2018-03-03T01:00:00.000Z",
"episode": {
"season": 3,
"number": 13,
"title": "Warning Shot",
"ids": {
"trakt": 2814272,
"tvdb": 6445735,
"imdb": "tt7462514",
"tmdb": 1429184,
"tvrage": 0
}
},
"show": {
"title": "Blindspot",
"year": 2015,
"ids": {
"trakt": 98980,
"slug": "blindspot",
"tvdb": 295647,
"imdb": "tt4474344",
"tmdb": 62710,
"tvrage": 44628
}
} }, {
"first_aired": "2018-03-03T01:00:00.000Z",
"episode": {
"season": 2,
"number": 16,
"title": "Hammock + Balcony",
"ids": {
"trakt": 2874663,
"tvdb": 6535389,
"imdb": "tt7820776",
"tmdb": 1428050,
"tvrage": 0
}
},
"show": {
"title": "MacGyver",
"year": 2016,
"ids": {
"trakt": 107792,
"slug": "macgyver-2016",
"tvdb": 311902,
"imdb": "tt1399045",
"tmdb": 67133,
"tvrage": {}
}
} } ]
I'm trying to get the "episode -> season" and "episode - > number"
This is the code ive been working with and also a fiddle below.
string json = "[{\"first_aired\":\"2018-03-03T01:00:00.000Z\",\"episode\":{\"season\":3,\"number\":13,\"title\":\"Warning Shot\",\"ids\":{\"trakt\":2814272,\"tvdb\":6445735,\"imdb\":\"tt7462514\",\"tmdb\":1429184,\"tvrage\":0}},\"show\":{\"title\":\"Blindspot\",\"year\":2015,\"ids\":{\"trakt\":98980,\"slug\":\"blindspot\",\"tvdb\":295647,\"imdb\":\"tt4474344\",\"tmdb\":62710,\"tvrage\":44628}}},{\"first_aired\":\"2018-03-03T01:00:00.000Z\",\"episode\":{\"season\":2,\"number\":16,\"title\":\"Hammock + Balcony\",\"ids\":{\"trakt\":2874663,\"tvdb\":6535389,\"imdb\":\"tt7820776\",\"tmdb\":1428050,\"tvrage\":0}},\"show\":{\"title\":\"MacGyver\",\"year\":2016,\"ids\":{\"trakt\":107792,\"slug\":\"macgyver-2016\",\"tvdb\":311902,\"imdb\":\"tt1399045\",\"tmdb\":67133,\"tvrage\":null}}}]";
JArray obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(json);
foreach (var result in obj)
{
foreach (JObject tvshow in result["episode"])
{
string season_num = (string)tvshow["season"];
string episode_num = (string)tvshow["number"];
Console.WriteLine(season_num + " - " + episode_num );
}
}
https://dotnetfiddle.net/speUyL
Thank's for any help anyone can give me!

You actually have nested objects, so you will need to first extract the episode object and then from the episode you can access it's properties which are number and season etc:
foreach (var result in obj)
{
var episode = result["episode"];
Console.WriteLine(episode["season"]);
Console.WriteLine(episode["number"]);
}
This prints the result you are trying to do. Following is the updated fiddle demo:
https://dotnetfiddle.net/WN545C
A easy approach is to have DTO c# classes for your json and then Deserialize the json result in to List<T>. The classes for your json would be :
public class Ids
{
public int trakt { get; set; }
public int tvdb { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public int tvrage { get; set; }
}
public class Episode
{
public int season { get; set; }
public int number { get; set; }
public string title { get; set; }
public Ids ids { get; set; }
}
public class Ids2
{
public int trakt { get; set; }
public string slug { get; set; }
public int tvdb { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public object tvrage { get; set; }
}
public class Show
{
public string title { get; set; }
public int year { get; set; }
public Ids2 ids { get; set; }
}
public class Season
{
public DateTime first_aired { get; set; }
public Episode episode { get; set; }
public Show show { get; set; }
}
An easy way to get the classes generated is by using Json2CSharp.com or either using the Visual Studio feature which can paste JSON as C# classes using Paste Special.
and now you can deserialize and access each season data more better way:
var seasons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Season>>(json);
foreach (var season in seasons)
{
Console.WriteLine(season.episode.title);
Console.WriteLine(season.first_aired);
Console.WriteLine(season.episode.season);
Console.WriteLine(season.episode.number);
}
You can play with the demo for that here:
https://dotnetfiddle.net/hukLQI

Related

Deserialize json to List<object> in C#

I have the following JSON string
{
"data": [
{
"symbol": "1COV.GE",
"exposure": "0",
"makerExposure": "-2028",
"takerExposure": "2028",
"makerPnl": "447.6688",
"takerPnl": "-447.6688",
"makerPositions": [
{
"name": "IB_001",
"position": "-2028",
"vwap": "47.41",
"pnl": "447.6688"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "2028",
"vwap": "47.41",
"pnl": "-447.6688"
}
]
},
{
"symbol": "A",
"exposure": "0",
"makerExposure": "-10",
"takerExposure": "10",
"makerPnl": "-4.6",
"takerPnl": "4.6",
"makerPositions": [
{
"name": "IB_002",
"position": "-10",
"vwap": "136.78",
"pnl": "-4.6"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "10",
"vwap": "136.78",
"pnl": "4.6"
}
],
"total": 2
}
}
And my goal is to serialize it into a List of object from the NODE "Data":
I have the classes that map the data node fields:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public OZPositions makerPositions { get; set; }
public OZPositions takerPositions { get; set; }
}
Do you have any ideas how I can convert the node "data" to list of "ExPositions" objects, eg. List
I've did this but so far it throws an error
var positions = JsonSerializer.Deserialize<ExPositions>(json_string);
There is an error in your json - it's missing a closing ] for the array (I'll assume it's a typo).
The real problem is that you need a wrapper class to represent the data node of the json which should contain a list (or array) of ExPositions. The makerPositions and takerPositions should also become lists (or arrays) too. Add the following class and update the position properties of ExPositions:
public class Data
{
public List<ExPositions> data { get; set; }
}
// change positions to use a List too
public class ExPositions
{
...
public List<Positions> makerPositions { get; set; }
public List<Positions> takerPositions { get; set; }
}
Then you can deserialize using:
var result = JsonSerializer.Deserialize<Data>(json);
It's not clear where the ""total"": 2 property should be in your models (it's not clear in the json because of the issue I mentioned), you could add it to the Data class above (if it belongs there).
Online demo
Try with:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public Positions makerPositions { get; set; }
public Positions takerPositions { get; set; }
}
public class YourResult{
public ExPositions data { get; set; }
public int total { get; set; }
}
And then call:
var positions = JsonSerializer.Deserialize<YourResult>(json_string);
As haldo mentioned, there is a typo in your JSON. To quickly parse and validate your JSON data, you can use any online JSON parsers to validate your JSON data. I usually use the chrome extension JSON Viewer Pro.
Also, in the link that haldo provided to the .NET Fiddle for the demo, there is a trailing comma in JSON data which JSON deserializers might not ignore.
Here is the link to the edited demo that haldo provided.
Edited Demo

Nested array in Newtonsoft C#

I am using Newtonsoft package in C#.
I am trying to display all the items listed in a nested JSON array. I am having difficulty displaying the Name Jennifer Jones
This is what the JSON String Looks Like
"responseDetails": {
"pageoffset": 0,
"size": 950,
},
"data": [
{
"id": 473145,
"name": "Class of 2000",
"doc_prog":
{
"responseDetails":
{
"pageoffset": 0,
"size": 1,
},
"data": [
{
"name": "Jennifer Jones"
}
]
},
This is what my class looks like
public respDetails responseDetails { get; set; }
public class respDetails
{
public int pageoffset { get; set; }
public string size { get; set; }
}
public List<datas> data { get; set; } // Top level class attribute
public class datas
{
public int id { get; set; }
public string name__v { get; set; }
public Programs doc_prog { get; set; }
public class Programs
{
public respDetails responseDetails { get; set; }
public class respDetails
{
public int pageoffset { get; set; }
public int size { get; set; }
}
public List<datasprogram> data { get; set; } // Top level class attribute
public class datasprogram
{
public string name { get; set; }
}
}
}
This is how I set up the for loop to list all the items in the array
var jRelated = JsonConvert.DeserializeObject<JDocsClass>(strRelated);
foreach (var num in jRelated.data)
{
Console.WriteLine(" Page Offset " + num.doc_prog.responseDetails.pageoffset.ToString() + " " + num.doc_prog.data.ToString() );
}
This is the Program Output
Page Offset 0 System.Collections.Generic.List`1[storeAPI.JCorrespondenceDocsClass+datas+Programs+datasprogram]
So Instead of displaying "Jennifer Jones", I am displaying "System.Collections.Generic.List...."
I appreciate any help pointing me in the right direction
num.doc_prog.data.ToString()
Here what you are trying to print is list!
num.doc_prog.data[0].name
This will give you the desired result if there is atleast one element in the list (which is there in your example json), if there are multiple names then to display them you need to loop thru num.doc_prog.data
foreach(datasprogram data in num.doc_prog.data)
{
string name = data.name;
}

Flattening complex data into a single object

I am trying to abstract data from a complex and create a EventDto.
And I was able to do it using foreach but the syntax is dreadful.
Is there a better way of writing this code?
public class EventDtO
{
public string Id { get; set; }
public string Title { get; set; }
public string CategoryTitle { get; set; }
public DateTime DateTime { get; set; }
}
This is the complex object that i am trying to get the data from
public class RootObject
{
public List<Event> Events { get; set; }
}
public class Event
{
public string Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public List<Category> Categories { get; set; }
public List<Geometry> Geometries { get; set; }
}
public class Geometry
{
public DateTime Date { get; set; }
public string Type { get; set; }
public List<object> Coordinates { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Title { get; set; }
}
The mapping relationship i want is
EventDto.Id->Event.Id
EventDto.Title->Event.Title
Event.CategoryTitle->Category.Title
Event.DateTime->Geometry.Date
The Category class will only contain one value, but the geometry.Date can have multiple values.
So the output i want is:
Title Categories Date
"Iceberg B42" Sea and Lake Ice 2020-04-23T14:24:00Z
"Iceberg B42" Sea and Lake Ice 2017-09-15T00:00:00Z
I am able to get the correct information if i do the following code.
var Event = new List<EventDTO>();
foreach (var con in content.Events)
{
var data = new EventDTO
{
Title = con.Title,
Id = con.Id
};
foreach (var cat in con.Categories)
{
data.CategoriesTitle = cat.Title;
}
foreach (var geo in con.Geometries)
{
data.DateTime = geo.Date;
Event.Add(data);
}
}
An example of the json
{
"id": "EONET_2881",
"title": "Iceberg B42",
"description": "",
"categories": [
{
"id": 15,
"title": "Sea and Lake Ice"
}
]
"geometries": [
{
"date": "2017-04-21T00:00:00Z",
"type": "Point",
"coordinates": [ -107.19, -74.63 ]
},
{
"date": "2017-09-15T00:00:00Z",
"type": "Point",
"coordinates": [ -107.11, -74.08 ]
}
]
}
You weren't creating a new EventDTO for each Geometry. Wouldn't this lead to multiple records with the date of the last one? Is this what you are looking for;
var Event = content.Events.SelectMany(con =>
con.Geometries.Select(geo =>
new EventDTO
{
Title = con.Title,
Id = con.Id,
CategoriesTitle = con.Categories.FirstOrDefault().Title,
DateTime = geo.Date
})
).ToList();

Change existing JSON message format into new with fields in C#?

We have to do some changes in existing JSON response and it requires to modify existing c# code too. Till now I've tried but don't get the exact idea about the formatting as it is new for me.
Can any one please check and suggest the changes in below code?
Existing JSON message:
"hotel_room_types": {
"QUEEN": {
"code": "QUEEN",
"bed_type": {
"standard": [
5
],
"custom": []
},
"extra_bed_type": {
"standard": [
5
],
"custom": []
},
"accessibility": "compliant_with_local_laws_for_disabled",
"room_smoking_policy": "non_smoking"
}
}
In above JSON message we have to replace the "bed_type" and "extra_bed_type" with "bed_configurations" and "extra_bed_configurations" in below newly provided format by client:
"hotel_room_types": {
"QUEEN": {
"code": "QUEEN",
"bed_configurations": [
[{
"type": "standard",
"code": 3,
"count": 1
}],
[{
"type": "standard",
"code": 1,
"count": 2
}]
],
"extra_bed_configurations": [
[{
"type": "standard",
"code": 900302,
"count": 1
},
{
"type": "custom",
"name": "Rollaway with wheel locks and adjustable height",
"count": 1
}]
],
"accessibility": "compliant_with_local_laws_for_disabled",
"room_smoking_policy": "non_smoking"
}
}
Existing C# code to generate the JSON response message format:
public class HotelRoomType
{
public string code { get; set; }
public string name { get; set; }
public string description { get; set; }
public List<Photo> photos { get; set; }
public Amenities room_amenities { get; set; }
public string room_size { get; set; }
public string room_size_units { get; set; }
public BedType bed_type { get; set; }
public BedType extra_bed_type { get; set; }
public RoomViewType room_view_type { get; set; }
public string accessibility { get; set; }
public MaxOccupancy max_occupancy { get; set; }
public string room_smoking_policy { get; set; }
}
Below is the code to fill the data in required fields of JSON message its inside a method which contains lines of code so I get it separately:
HotelRoomType hotelrmtype = new HotelRoomType();
hotelrmtype.bed_type = Common.GetStandardBedTypeMappingID(rm.BedType);
if (rm.NumOfBed > 1)
hotelrmtype.extra_bed_type = hotelrmtype.bed_type; //same as bed type
hotelrmtypeDict.Add(rm.Code, hotelrmtype); //Binding Data into Dictionary.
GetStandardBedTypeMappingID() contains :
public static CRS.TripConnect.BedType GetStandardBedTypeMappingID(short code)
{
CRS.TripConnect.BedType tripConnectBedType = new CRS.TripConnect.BedType();
List<int> standardBedTypes = new List<int>();
List<object> customBedTypes = new List<object>();
tripConnectBedType.standard = standardBedTypes;
tripConnectBedType.custom = customBedTypes; //These is blank.
short id = 0;
switch (code)
{
case 10:
id = 3;
break;
case 20: // 20 Queen Q 5
id = 5;
break;
case 30: // 30 Double D 1
id = 1;
break;
case 40: // 40 Twin T 8
id = 8;
break;
}
standardBedTypes.Add(id);
return tripConnectBedType;
}
Update: With the help of #Sam Answer below I have modified the code:
public class HotelRoomType
{
//Added following properties
public List<List<Bed_Configurations>> bed_configurations { get; set; }
public List<List<Extra_Bed_Configurations>> extra_bed_configurations { get; set; }
}
Created two new classes as:
public class Bed_Configurations
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
}
public class Extra_Bed_Configurations
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
public string name { get; set; }
}
Now the question is: How to fill these two list?
I'm trying to achieve this like below but it is giving me conversion error.
Bed_Configurations bdConfig = new Bed_Configurations();
bdConfig.type = "Standard";
bdConfig.code = Common.GetStandardBedTypeMappingIDnew(rm.BedType);
bdConfig.count = rm.NumOfBed;
hotelrmtype.bed_configurations.Add(bdConfig);
Error Message:
Please Advise the changes in the above code so as to get the required JSON message. Appreciate your help!
public class RootClass
{
public HotelRoomType hotel_room_types { get; set; }
}
public class HotelRoomType
{
public BedModelAndDetails QUEEN { get;set; }
}
public class BedModelAndDetails
{
public string accessibility { get; set; }
public string room_smoking_policy { get; set; }
public string code { get; set; }
//public BedType bed_type { get; set; }
public object bed_type { get; set; }
//public BedType extra_bed_type { get; set; }
public object extra_bed_type { get; set; }
public List<List<BedConfiguration>> bed_configurations { get; set; }
public List<List<BedConfiguration>> extra_bed_configurations { get; set; }
}
public class BedType
{
public List<int> standard { get; set; }
public List<int> custom { get; set; }
}
public class BedConfiguration
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
}
[TestMethod]
public void ReplaceJson()
{
var inputJson1 = "{\"hotel_room_types\": {\"QUEEN\": {\"code\": \"QUEEN\", \"bed_type\": {\"standard\": [5],\"custom\": [] }, \"extra_bed_type\": { \"standard\": [5], \"custom\": [] },\"accessibility\": \"compliant_with_local_laws_for_disabled\", \"room_smoking_policy\": \"non_smoking\" } " +"}}";
var input1 = JsonConvert.DeserializeObject<RootClass>(inputJson1, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
var inputJson2 = "{\"hotel_room_types\": {\"QUEEN\": {\"code\": \"QUEEN\",\"bed_configurations\": [[{\"type\": \"standard\",\"code\": 3,\"count\": 1}],[{\"type\": \"standard\",\"code\": 1,\"count\": 2}]],\"extra_bed_configurations\": [[{\"type\": \"standard\",\"code\": 900302,\"count\": 1},{\"type\": \"custom\",\"name\": \"Rollaway with wheel locks and adjustable height\",\"count\": 1}]],\"accessibility\": \"compliant_with_local_laws_for_disabled\",\"room_smoking_policy\": \"non_smoking\"} }}";
var input2 = JsonConvert.DeserializeObject<RootClass>(inputJson2);
//var finalInput = new RootClass();
//finalInput.hotel_room_types = inputJson1
//input1.hotel_room_types.QUEEN.bed_configurations = input2.hotel_room_types.QUEEN.bed_configurations;
//input1.hotel_room_types.QUEEN.extra_bed_configurations = input2.hotel_room_types.QUEEN.extra_bed_configurations;
input1.hotel_room_types.QUEEN.bed_type = input2.hotel_room_types.QUEEN.bed_configurations;
input1.hotel_room_types.QUEEN.extra_bed_type = input2.hotel_room_types.QUEEN.extra_bed_configurations;
}
Does this help?

Json.Net deserialize JSON objects with index as name [duplicate]

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Closed 8 years ago.
I am attempting to parse JSON from a web service using Json.NET, the web service returns data in the following format:
{
"0": {
"ID": 193,
"Title": "Title 193",
"Description": "Description 193",
"Order": 5,
"Hyperlink": "http://someurl.com"
},
"1": {
"ID": 228,
"Title": "Title 228",
"Description": "Description 228",
"Order": 4,
"Hyperlink": "http://someurl.com"
},
"2": {
"ID": 234,
"Title": "Title 234",
"Description": "Description 234",
"Order": 3,
"Hyperlink": "http://someurl.com"
}
}
I used json2sharp to generate a class from the JSON:
public class __invalid_type__0
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Order { get; set; }
public string Hyperlink { get; set; }
}
public class __invalid_type__1
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Order { get; set; }
public string Hyperlink { get; set; }
}
public class __invalid_type__2
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Order { get; set; }
public string Hyperlink { get; set; }
}
public class RootObject
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
public __invalid_type__2 __invalid_name__2 { get; set; }
}
I then cleaned up the class and was left with the following:
public class Articles
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Order { get; set; }
public string Hyperlink { get; set; }
}
public class FeaturedArticles
{
public List<Articles> articles { get; set; }
}
When I attempt to load the data into my singleton for use in the app:
private void fetchFeaturedArticles()
{
var client = new RestClient (_featuredArticlesJsonUrl);
var request = new RestRequest (Method.GET);
var response = client.Execute (request);
_featuredArticles = JsonConvert.DeserializeObject<FeaturedArticles> (response.Content);
foreach (Articles a in _featuredArticles.Articles)
Console.WriteLine (a.Title);
}
I find that the Articles do not get deserialized.
I've verified that the JSON data is returned from the web service. I believe the issue exists in the structure of my JSON feed, where each item returned from the feed is given a name which equals the index the item is being returned as.
I am new to using Json.NET so I'm not sure how I should proceed; I cannot change the structure of the JSON feed but need to consume it's data. Anyone have any recommendations?
You don't need FeaturedArticles class, you can deserialize the JSON into a Dictionary<string, Articles> like this:
private void fetchFeaturedArticles()
{
var client = new RestClient (_featuredArticlesJsonUrl);
var request = new RestRequest (Method.GET);
var response = client.Execute (request);
Dictionary<string, Articles> _featuredArticles = JsonConvert.DeserializeObject<Dictionary<string, Articles>>(response.Content);
foreach (string key in _featuredArticles.Keys)
{
Console.WriteLine(_featuredArticles[key].Title);
}
}
Demo: https://dotnetfiddle.net/ZE1BMl

Categories

Resources