0

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&#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
            }
          ]
        }
      ]
    }
  ]
}

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?

1
  • Do you need the entire Content object or just a list of Content2 that match your criteria? Commented Nov 21, 2017 at 13:27

2 Answers 2

1

You haven't shown what ContentJsonNew looks like, but based on your example Linq query I will assume a basic structure like so:

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
}

In this List of Lists scenario, to actually get to Content2, you're going to have to flatten each of your lists before you can filter the type. You can use SelectMany to do this.

var content2Matches = json.Sections
                .SelectMany(x => x.Content)
                .SelectMany(x => x.ContentToSearch)
                .Where(x => x.Type.ToLower() == "image" || x.Type.ToLower() == "button");
Sign up to request clarification or add additional context in comments.

Comments

0

Hacky way out I would enhance json string before parsing:

  "Segments": null,
  "Content": [

String replace to:

  "Segments": null,
  "ContentOuter": [

then name outer class ContentOuter and only then parse json. Downsides include I do not know how much data you have there so replace might be crappy. If at some point layout with segments changes it will also break. So you have to know if you can gamble that way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.