Home > other >  Netwonsoft JSON deserialize into List with named entries
Netwonsoft JSON deserialize into List with named entries

Time:10-02

I want to deserialize this JSON:

{
  "Home1": [
    {
      "name": "Hans",
      "age": 20
    },
    {...}
  ],
  "Home2": [
    {...},
    {...}
  ]
}

into an List<House> with these classes:

class Person {
  public string Name { get; set; }
  public int Age { get; set; }
}
class House : List<Person> {
  public string Name { get; set; }
}

How can I tell Newtonsoft JSON that House.Name should be for the key (Home1)?

PS: The class structure is not fixed, but I need the name of the house to be a property in a class.

CodePudding user response:

Leaving behind the rational behind the idea of inheriting a list and your class structure you surely can create custom converter but I would argue much easier option would be to deserialize json into Dictionary<string, House> (which can represent your json structure) and then manually map the names:

var result = JsonConvert.DeserializeObject<Dictionary<string, House>>(json);
foreach (var kvp in result)
{
    kvp.Value.Name = kvp.Key;
}

var houses = result.Values;

CodePudding user response:

try this

   var deserializedJson = JsonConvert.DeserializeObject<Dictionary<string, List<Person>>>(json);
    
    var houses=new List<House>();
    
    foreach (var element in deserializedJson)
    {
        houses.Add(new House { Name = element.Key, Persons = element.Value} );
    }
   var result=JsonConvert.SerializeObject(houses);
 }

result

[
  {
    "Name": "Home1",
    "Persons": [
      {
        "Name": "Hans",
        "Age": 20
      }
    ]
  },
  {
    "Name": "Home2",
    "Persons": [
      {
        "Name": "John",
        "Age": 22
      }
    ]
  }
]

classes

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class House 
{
public string Name { get; set; }
public List<Person> Persons {get; set;}
}

CodePudding user response:

Json parsing/deserialization doesn't work this way.

You must have the class of same structure, of what the json is.

Your source json is object having property Home1, Home2 etc.

But you directly expecting it to convert into another structure.

I recommend first convert the json in Real object with the structure of json. Then loop through the property and fill it with loop in List.

Also can you please explain what this class is supposed to do.

class House : List<Person> { public string Name { get; set; } }

For me it doesn't make any sense.

CodePudding user response:

An answer based on you requirement,

How about we clean your JSON string before deserializing and spicing it up with REGEX?

string json = @"{
    ""Home1"": [
    {
        ""name"": ""Hans"",
        ""age"": 20
    },
    {
        ""name"": ""Hans"",
        ""age"": 20
    },
    ],
    ""Home2"": [
    {
        ""name"": ""Hans"",
        ""age"": 20
    },
    {
        ""name"": ""Hans"",
        ""age"": 20
    },
    ]
}";

/*Replaces all Home   Digit combination to Home*/
json = Regex.Replace(json, @"Home\d*", "Home");

/* Output
{
    "Home": [
    {
        "name": "Hans",
        "age": 20
    },
    {
        "name": "Hans",
        "age": 20
    },
    ],
    "Home": [
    {
        "name": "Hans",
        "age": 20
    },
    {
        "name": "Hans",
        "age": 20
    },
    ]
}
*/

  • Related