【问题标题】:Json parsing with Linq使用 Linq 解析 Json
【发布时间】: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&#39;s top trends and we&#39;ve got the scoop on the latest must-haves straight from fashion week to your closet.&nbsp;</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&#39;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 &nbsp;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&#39;s wardrobe. Solid colors are especially handy to mix and match and reuse for daywear or even a night out.&nbsp;</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 列表?

标签: c# json linq


【解决方案1】:

您还没有展示 ContentJsonNew 的样子,但根据您的示例 Linq 查询,我将假设一个基本结构如下:

public class ContentJsonNew
{
    public List<Section> Sections { get; set; }
}

public class Section
{
    public List<Content> Content { get; set; }
}

public class Content
{
    public List<Content2> ContentToSearch { get; set; }
}

public class Content2 {
public string Type { get; set; }
//other properties
}

在这个 List of Lists 场景中,要真正到达 Content2,您必须先展平每个列表,然后才能过滤类型。您可以使用 SelectMany 来执行此操作。

var content2Matches = json.Sections
                .SelectMany(x => x.Content)
                .SelectMany(x => x.ContentToSearch)
                .Where(x => x.Type.ToLower() == "image" || x.Type.ToLower() == "button");

【讨论】:

    【解决方案2】:

    Hacky 出路我会在解析之前增强 json 字符串:

      "Segments": null,
      "Content": [
    

    字符串替换为:

      "Segments": null,
      "ContentOuter": [
    

    然后命名外部类 ContentOuter,然后才解析 json。 缺点包括我不知道你有多少数据,所以替换可能很糟糕。如果在某些时候带有段的布局发生变化,它也会中断。所以你必须知道你是否可以这样赌博。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多