I have the following Json structure and used Json2csharp to convert (json2csharp.com):
{
"TemplateId":1,
"Sections": [
{
"Id": "one-col-feature",
"ReadOnly": false,
"Lists": null,
"Segments": null,
"Content": [
{
"Content": [
{
"Type": "image",
"Halign": "center",
"IsFullWidth": true,
"FileName": "image.png",
"Alt": "",
"Href": "",
"OriginalWidth": 125,
"OriginalHeight": 125,
"Validation": null
},
{
"Contents": "<h1 style=\"text-align:center\">This season is all about denim!</h1>",
"Type": "text",
"Validation": null
},
{
"Contents": "<p>Have you checked your denim inventory lately? Denim is one of this season's top trends and we've got the scoop on the latest must-haves straight from fashion week to your closet. </p>",
"Type": "text",
"Validation": null
},
{
"Text": "Our guide to denim",
"Lines": [
"Our guide to denim"
],
"Href": "google.com",
"Width": 520,
"Height": 53,
"Halign": "left",
"Type": "button",
"Validation": null
}
]
}
]
},
{
"Id": "two-col",
"ReadOnly": false,
"Lists": null,
"Segments": null,
"Content": [
{
"Content": [
{
"Type": "image",
"Halign": "center",
"IsFullWidth": true,
"FileName": "photo-1423753623104-718aaace6772.jpg",
"Alt": "",
"Href": "",
"OriginalWidth": 454,
"OriginalHeight": 455,
"Validation": null
},
{
"Contents": "<h1>Henleys</h1><p><strong>Every man needs one</strong></p><p>The Henley is this season's top pick for the man that wants an alternative to the classic v-neck t-shirt. Whether you pair it with a blazer for a sophisticated look or wear it plain for a more casual look, the Henley is a great way to upgrade your wardrobe.</p>",
"Type": "text",
"Validation": null
},
{
"Text": "Shop Henleys",
"Lines": [
"Shop Henleys"
],
"Href": "google.com",
"Width": 210,
"Height": 53,
"Halign": "left",
"Type": "button",
"Validation": null
}
]
},
{
"Content": [
{
"Type": "image",
"Halign": "center",
"IsFullWidth": true,
"FileName": "outwear1.jpg",
"Alt": "",
"Href": "",
"OriginalWidth": 454,
"OriginalHeight": 455,
"Validation": null
},
{
"Contents": "<h1>Cardigans</h1><p><strong>Making a comeback</strong></p><p>This season, cardigans in earth tones are all the rage. Great for any occasion, the cardigan is an essential item for every woman's wardrobe. Solid colors are especially handy to mix and match and reuse for daywear or even a night out. </p>",
"Type": "text",
"Validation": null
},
{
"Text": "Shop Cardigans",
"Lines": [
"Shop Cardigans"
],
"Href": "google.com",
"Width": 210,
"Height": 53,
"Halign": "left",
"Type": "button",
"Validation": null
}
]
}
]
}
]
}
I have converted this to a c# object which I want to parse.
ContentJsonNew json = JsonConvert.DeserializeObject<ContentJsonNew>(row["contentjson"].ToString());
Unfortunately it comes in a format where the child property is the same as parent - so i called it Content to search. Specifically, that part of the object is:
public class Content
{
[JsonProperty("Content")]
public List<Content2> ContentToSearch { get; set; }
}
I specifically wnt to find all items of Content2 where type is "image" or type is "button"
I tried:
ContentJsonNew json = JsonConvert.DeserializeObject<ContentJsonNew>(row["contentjson"].ToString());
var test = json.Sections.Where(m => m.Content.Any(d => d.ContentToSearch.Any(e => e.Type == "image" || e.Type == "button")));
This doesnt work. Any other suggestions for these nested objects?