C# List<List<T>> - c#

I have a class with the following property: Education.cs
Apologize for the perhaps basic question
public List<List<Education>>data { get; set; }
public class Education
{
public From school { get; set; }
public From year { get; set; }
public string type { get; set; }
}
This is the class I have defined for deserializing json string
From is another .cs file
public string id { get; set; }
public string name { get; set; }
Here is my json string
education": [
{
"school": {
"id": "107751089258341",
"name": "JNTU, Kakinada, India"
},
"year": {
"id": "132393000129123",
"name": "2001"
},
"type": "College"
},
{
"school": {
"id": "103710319677602",
"name": "University of Houston"
},
"year": {
"id": "113125125403208",
"name": "2004"
},
"type": "Graduate School"
}
]
Can someone tell me how to access the members of Education(school, year)? It could be a piece of cake for you.
In my aspx.cs,
I have to write a foreach or any other to access my variables, school.name, year.name
Will have to work this access of class members into my aspx.cs
url= "https://graph.facebook.com/me?fields=education&access_token=" + oAuth.Token;
json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
List<Education>??? = js.Deserialize<List<??>(json)
Thanks
Smitha

You need two foreach loops inside of eachother; one for each level of List<>.

#Slaks solution should work for you. Though I believe your data is better represented as List<Education> (or better still, IEnumerable<Education>) and what you may want to do is to Flatten it out.
It would be better to flatten it out at the source, to make sure your code is cleaner at other places.
If you are on .NET 3.5 you can do it like
var flattenData = data.SelectMany(x => x);
If you are on pre .NET 3.5 / C# 3.0, you can do it like this
//Your original code block
{
IEnumerable<Education> flattenData = FlattenMyList(data);
//use flatten data normally
}
IEnumerable<T> FlattenMyList<T> (List<List<T> data){
foreach(List<T> innerList in data){
foreach(T item in innerList){
yield return item;
}
}
yield break;
}

Related

Cross-referencing two parts of one JSON file in C#?

Newbie question. I have a semi-complex JSON file that I have parsed in my program. The relevant parts are below:
{
"version": "36",
"released": "20220223",
"samples": {
"Samp1": [
{
"code": "A01",
"status": "Optimal",
"bestBy": "20210918",
"expires": "20211018",
"elementKeys": {
"H": [
"Hydrogen-std1-slt4"
]
}
},
{
"code": "A02",
"status": "Optimal",
"bestBy": "20211201",
"expires": "20220501",
"elementKeys": {
"H": [
"Hydrogen-std1-slt5"
]
}
},
{
"code": "A03",
"status": "Optimal",
"bestBy": "20230201",
"expires": "20230801",
"elementKeys": {
"H": [
"Hydrogen-std1-slt6"
]
}
}
],
"Samp2": [ ...
"Samp3": [ ...
"Samp4": [ ...
},
"element": {
"Hydrogen-std1-slt4": {
"format": "1.0",
"position": 4,
"standard": "std1",
...
...
}
What I need to do is populate some windows form controls with data from this file. However, thanks to the odd architecture, I'm a little frustrated over how to populate the controls for "code", "status", "bestBy", etc contained within the "samples" arrays (Samp1, 2, 3, and 4).
Only some of the samples are relevant and the relevancy is defined by whether or not the name of the element key is found further below in the JSON file. So, using the example above, within "element", the object "Hydrogen-std1-slt4" is found in the body of the JSON file with its own key-value pairs. I would like the program to see that and recognize that "Hydrogen-std1-slt4" is also found within the object in the "Samp1" array with the code "A01", the status of "Optimal", the best-by date of "20210918", and that it expires on "20211018". Since it isn't found in any other place in "Samp1", the program can ignore the other objects.
What would be the easiest, most logical way to go about making a conditional for that?
try this code
var jsonParsed = JObject.Parse(json);
string[] elementKeys = ((JObject)jsonParsed["element"]).Properties().Select(x => x.Name).ToArray();
List<Samp> data= GetData(((JObject) jsonParsed["samples"]), elementKeys);
public List<Samp> GetData(JObject samples, string[] elementKeys)
{
List<Samp> result = new List<Samp>();
foreach (var element in samples.Properties())
foreach (var item in element.Value)
if ( item["elementKeys"]["H"]!=null
&& item["elementKeys"]["H"].ToObject<string[]>()
.Any(x => elementKeys.Contains(x)) )
result.Add(item.ToObject<Samp>());
return result;
}
classes
public class Samp
{
public string code { get; set; }
public string status { get; set; }
public string bestBy { get; set; }
public string expires { get; set; }
public ElementKeys elementKeys { get; set; }
}
public class ElementKeys
{
public List<string> H { get; set; }
}

Get specific data from a JSON

Suppose there is a JSON structure like below
{
"v": "2021",
"Outeritems": [
{
"items": [
{
"c": "r",
"KeyOne": "DataOne",
"KeyTwo": "DataTwo",
"items": [
{
"c": "r",
"KeyOne": "DataThree",
"KeyTwo": "DataFour",
"v": "F",
"h": "N",
"l": "N:"
},
{
"c": "r",
"KeyOne": "DataFive",
"KeyTwo": "DataSix",
"v": "T"
}
]
}
]
}
]
}
How can I read all KeyOne and its corresponding KeyTwo(line below KeyOne) using linq or some method. They may be nested in any of items array. We need to get all such properties as a dictionary or key value pair like that. Thanks for help.
Well instead commenting out, lets build an aprox answer.
Actually the better aproach is deserialize the JSON to a class with only the relevant properties, instead trying to use all the JSON structure.
Like:
private class Item
{
[JsonProperty("KeyOne")]
public string KeyOne { get; set; }
[JsonProperty("KeyTwo")]
public string KeyTwo { get; set; }
[JsonProperty("items")]
public List<Item> Items { get; set; }
}
private class Outeritem
{
[JsonProperty("items")]
public List<Item> Items { get; set; }
}
private class Root
{
[JsonProperty("Outeritems")]
public List<Outeritem> Outeritems { get; set; }
}
Then deserialize like:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
Then for transverse the tree you can use a recursive aproach (just because a JSON string is a rather finite structure, not a good aproach in ALL cases)
List<string> KeyOneValues = new List<string>();
List<string> KeyTwoValues = new List<string>();
trasverseNode(List<Item> item)
{
if (item.KeyOne != null) KeyOneValues.Add(item.KeyOne);
if (item.KeyTwo != null) KeyTwoValues.Add(item.KeyTwo);
foreach (Item child in item.Items)
{
trasverseNode(child); //<-- recursive
}
}

JSON .NET deserialize into complex type with object key/name

I'm using Newtonsoft JSON .NET in my project for most of my API/JSON usage, and usually deserialize or convert into a complex type for easier usage. The problem is that I can't seem to figure out a good way to handle an object with a name/key that is dynamic (ie it is different every time).
The JSON I need to parse isn't exactly made to standard, but currently it's not in my power to change it. The relevant JSON is below (I didn't include the whole structure).
"orders": {
"32f5c31d-a851-40cf-a8bb-2fea4bd27777": {
"orderId": "ord12345",
"state": "new",
"sellAmount": "200",
"paymentMethod": "buyMethod2",
"email": "example#email.com",
"pinOrPhonenumber": "180012345",
"timestamp": "2014-10-01T07:13:33.868Z"
},
"123d98d5-19b1-4cb3-8ab3-b991650b134c": {
"orderId": "ord12346",
"state": "pending",
"sellAmount": "1200",
"paymentMethod": "buyMethod2",
"email": "example#email.com",
"pinOrPhonenumber": "180012345",
"timestamp": "2014-10-01T07:13:33.868Z"
}
}
The code currently looks like:
var jResponse = Newtonsoft.Json.Linq.JObject.Parse(rResponse.Content);
var jOrders = jResponse["orders"];
foreach (var jChild in jOrders) {
string sOrderGUID = ""; //The name/key of the object
var jOrder = jChild.First.ToObject<Order>();
//Handle the order data, etc
}
And the class:
public class Order {
[JsonProperty("orderId")]
public string sOrderID { get; set; }
[JsonProperty("state")]
public string sState { get; set; }
[JsonProperty("sellAmount")]
public decimal nAmount { get; set; }
[JsonProperty("paymentMethod")]
public string sPaymentMethod { get; set; }
[JsonProperty("email")]
public string sEmail { get; set; }
[JsonProperty("phonenumber")]
public string sPhonenumber { get; set; }
[JsonProperty("paymentMessage")]
public string sPaymentMessage { get; set; }
[JsonProperty("timestamp")]
public DateTime dTimestamp { get; set; }
}
Been looking into converting it into a JObject and using the Properties to get the key out, but I would prefer a cleaner way to do it if possible. Any pointers?
Update: The Dictionary solution worked out well for the order situation, but I then ran into another problem when trying to apply the same method to another part of the JSON.
"rates": {
"sek": {
"name": "Swedish Krona",
"usd": {
"name": "US Dollar",
"rates": {
"2014-10-01T12:14:26.497Z": 0.138794,
"2014-10-01T12:17:51.143Z": 0.138794,
"2014-10-01T12:26:26.827Z": 0.138794,
"2014-10-01T12:51:03.347Z": 0.138794
}
}
},
"usd": {
"name": "US Dollar",
"sek": {
"name": "Swedish Krona",
"rates": {
"2014-10-01T12:14:28.763Z": 7.20753,
"2014-10-01T12:17:52.667Z": 7.20753,
"2014-10-01T12:26:28.477Z": 7.20753,
"2014-10-01T12:51:04.963Z": 7.20753
}
}
}
}
The problem being that I need to collect the "name" value as well as the rest but the currency code is variable. Considered using nesting Dictionary, but can't seem to get a way to capture the "name".
Yes dictionary is a better answer for this.
var orderWrapper = JsonConvert.DeserializeObject<OrderWrapper>(json);
IEnumerable<Order> orders = result.GetOrders();
public class OrderWrapper
{
[JsonProperty("orders")]
private Dictionary<string, Order> _orders;
public IEnumerable<Order> GetOrders()
{
return _orders.Values;
}
}
i tried to parse your JSON in the below line
var result = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,Order>>>(jsonString);
in your json string, you have first a key called "orders", this will match the string key if the outer dictionary, then the inner dictionary string key will match the order key and the value will be parsed as the order

How to deserialize json string in windows phone?

I just got my json response as a string.My json is given below,
"code": 0,
"message": "success",
"students": {
"details":{
"hjeke": {
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good",
},
"Second": {
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad",
}
}
}
Like hjeke and Second there are many key value pairs,how can i deserialize my json using Newtonsoft.json
Try to understand my solution in your previous question
How to deserialize json data in windows phone?
Your first JSON in that question was good and simple to use.
JSON, where field names are unique not convinient to deserialize. So, you got problems such as public class Hjeke and public class Second for each instance, when you use code generator.
Use JSON-structure with list of students:
"code": 0,
"message": "success",
"students": [
{
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good",
},
{
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad",
}]
is good and flexible structure. Using this, you don't need to parse not obvious fields like
"details":{
"hjeke": {
and so on.
And work with them using classes, from my previous answer. The main idea - you need list of objects. public List<StudentDetails> students. Then, all students objects deserialized in List, which is easy to use.
As everybody mentioned your json seems to be very unflexible, huh.
You can extract the data you are interested in.
So this is your model:
public class StudentDetails
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
And this is how you can extract it:
var jsonObj = JObject.Parse(str);
// get JSON result objects into a list
var results = jsonObj["students"]["details"].Children().Values();
// serialize JSON results into .NET objects
var details = new List<StudentDetails>();
foreach (JToken result in results)
{
var st = result.ToString();
var searchResult = JsonConvert.DeserializeObject<StudentDetails>(st);
details.Add(searchResult);
}
I'm using a newtonsoft.json library here.
Your Response string has some mistakes man, its not a valid json
just small modification to be done as below:
{
"code": 0,
"message": "success",
"students": {
"details": {
"hjeke": {
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good"
},
"Second": {
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad"
}
}
}
}
you can make out the difference
Now Follow these steps:
1.Go to this link Json to C#
2.place your Json string there and generate C# class object
3.Now create this class in your solution
4.Now deserialize As below
var DeserialisedObject = JsonConvert.DeserializeObject<Your Class>(YourJsonString);
First, create the classes:
public class Hjeke
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
public class Second
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
public class Details
{
public List<Hjeke> hjeke { get; set; }
public List<Second> Second { get; set; }
}
public class Students
{
public List<Details> details { get; set; }
}
public class RootObject
{
public int code { get; set; }
public string message { get; set; }
public List<Students> students { get; set; }
}
After that, use JSON.NET to deserialize:
var deserialized = JsonConvert.DeserializeObject<Class1>(YourStringHere);
Do you have any influence over the json response? Details should probably be a JSONArray in this case, not an object with a varying amount of properties, since I assume that's what you mean is the issue here.

Deserialize JSON dynamically to derived types based on content?

In my little library I'm writing as a side project, I'm using RestSharp to get Json from a web API. The deserialization into model classes works fine for simple types, but there are endpoints where the resulting type is unknown (or unclear) at request time.
Specifically its the GuildWars2 API v1 and one example would be item data. There are of course basic properties every item has and additional values are set depending on the queried item.
For example a weapon has some modifiers and so on.
My idea was to create an abstract Item class for all basic properties and derive from that class for all the existing subtypes of items adding needed properties. My problem is, how to decide which subclass to deserialize into. I already read about various methods involving adding a qualified type name to the Json string but since I only get the data from an API, I cannot influence the response im getting.
I would ideally always want to deserialize into the base class Item with the deserializer deciding the actual type via the given properties contained in the Json string.. Is this a possibility? Or is there a better way to solve this problem?
Thanks in advance
EDIT: This is an example Json of a weapon:
{
"item_id": "30704",
"name": "Twilight",
"description": "",
"type": "Weapon",
"level": "80",
"rarity": "Legendary",
"vendor_value": "100000",
"icon_file_id": "456031",
"icon_file_signature": "CE3AF0B7B9BB6244726779F5B6A930541BA6C15F",
"game_types": ["Activity", "Dungeon", "Pve", "Wvw"],
"flags": ["HideSuffix", "NoSell", "SoulBindOnUse"],
"restrictions": [],
"weapon": {
"type": "Greatsword",
"damage_type": "Physical",
"min_power": "995",
"max_power": "1100",
"defense": "0",
"infusion_slots": [],
"infix_upgrade": {
"attributes": [
{
"attribute": "Power",
"modifier": "179"
},
{
"attribute": "Power",
"modifier": "179"
}
]
},
"suffix_item_id": "24599"
}
}
And this would be an armor piece:
{
"item_id":"500",
"name":"Carrion Conjurer Chest of Dwayna",
"description":"",
"type":"Armor",
"level":"68",
"rarity":"Rare",
"vendor_value":"257",
"icon_file_id":"61023",
"icon_file_signature":"76CD08463A05730071D400254141B50E570662D3",
"game_types":["Activity", "Dungeon", "Pve", "Wvw"],
"flags":["SoulBindOnUse"],
"restrictions":[],
"armor": {
"type":"Coat",
"weight_class":"Light",
"defense":"221",
"infusion_slots":[],
"infix_upgrade": {
"attributes": [
{
"attribute":"ConditionDamage","modifier":"70"
},
{
"attribute":"Power","modifier":"50"
}
]
},
"suffix_item_id":"24767"
}
}
I'd like to deserialize based on the content of the property "Type", but I cannot figure out, how that would work =/
You can parse your JSON data with JObject, which then can be used as an associative array.
string json = #"{
"item_id": "30704",
"name": "Twilight",
"description": "",
"type": "Weapon",
...
}";
JObject o = JObject.Parse(json);
Item item = null;
switch (o["type"])
{
case "Weapon":
item = JsonConvert.DeserializeObject<Weapon>(json);
break;
case "Armor":
item = JsonConvert.DeserializeObject<Armor>(json);
break;
default:
// throw error?
}
and then you'd have a base class as such:
public class Item
{
public string item_id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string type { get; set; }
...
// List all common properties here
public Item() { }
// It's important to have the public default constructor implemented
// if you intend on overloading it for your own purpose later
}
And your Weapon class could be as such:
public class Weapon : Item // Weapon extends Item
{
public WeaponDetails weapon { get; set; }
public Weapon() { }
}
And Weapon Details:
public class WeaponDetails
{
public string type { get; set; }
public string damage_type { get; set; }
public int min_power { get; set; }
public int max_power { get; set; }
...
public WeaponDetails() { }
}

Categories