【发布时间】:2017-11-21 13:03:42
【问题描述】:
我有以下Json结构,使用Json2csharp进行转换(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
}
]
}
]
}
]
}
我已将其转换为我要解析的 c# 对象。
ContentJsonNew json = JsonConvert.DeserializeObject<ContentJsonNew>(row["contentjson"].ToString());
不幸的是,它采用子属性与父属性相同的格式 - 所以我将其称为搜索内容。具体来说,对象的那部分是:
public class Content
{
[JsonProperty("Content")]
public List<Content2> ContentToSearch { get; set; }
}
我特别想查找类型为“图像”或类型为“按钮”的 Content2 的所有项目
我试过了:
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")));
这不起作用。对于这些嵌套对象还有其他建议吗?
【问题讨论】:
-
您需要整个 Content 对象还是只需要符合您条件的 Content2 列表?