1

I am working on a model that has a list within it.

I need to flatten the list into the main object that is a JSON file.

So this is what I need to flatten it as: Example:

{
name: Test1
startDate: 6/6/2020
endDate: 6/7/2020
assetId: 8
locationId: 10
}

The C# class model is like this:

public string name { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public List<Resources> ResourcesList { get; set; }

This would the class Resources:

public string Type { get; set; }
public int Id { get; set; }

Then the values for Sources would be this:

new Resources { Type = "assetId", Id = 8 }
new Resources { Type = "locationId", Id = 10 }
pg2727
  • 149
  • 1
  • 10
  • 1
    Given that there is a list of sources, you should either add an example with multiple sources, or explicitly state that there is only ever a a single source – Ted Brownlow Jun 07 '20 at 02:47
  • @TedBrownlow, I did. It is at the bottom of Type/Id of assetId and locations. Then I need that flattened out to the first example in my post and instead of in a list. – pg2727 Jun 07 '20 at 03:19
  • Is JSON the output format? Your snippet at the top isn't JSON – Ted Brownlow Jun 07 '20 at 04:17
  • @TedBrownlow: Yes, sorry I just copied the inner section. Basically I've tried this: `[JsonConverter(typeof(ResourceConverter))] public IList ScheduleEventResources { get; set; } = new List();` So everything within that list I want to place in the same level as ScheduleEventResource of the object. – pg2727 Jun 07 '20 at 04:20
  • @TedBrownlow: I tried this article as well and it doesn't seem to work for what I need: https://passos.com.au/converting-json-object-into-c-list/ – pg2727 Jun 07 '20 at 04:27
  • @TedBrownlow, I think I'm close. I tried `[JsonProperty] without passing it a name and it puts it at one level down, but if it is in a dictionary (string, int) pair, it has exactly what I want to see, just still in the list. I want to push all items in the dictionary up the level with all the other properties. – pg2727 Jun 07 '20 at 05:28

0 Answers0