3

I've got the following json document:

{
  "name": "bert",
  "Bikes": {
    "Bike1": {
      "value": 1000,
      "type": "Trek"
    },
    "Bike2": {
      "value": 2000,
      "type": "Canyon"
    }
  }
}

With potentially other bikes like Bike3...BikeN. I want to deserialize to C# objects. Problem is that in the deserialization step the bikes data is completely lost, resulting in a null Bikes collection.

Code to reproduce:

[Test]
public void FirstCityJsonParsingTest()
{
    var file = @"./testdata/test.json";
    var json = File.ReadAllText(file);

    var res = JsonConvert.DeserializeObject<Person>(json);
    Assert.IsTrue(res.Name == "bert");
    // next line is failing, because res.Bikes is null...
    Assert.IsTrue(res.Bikes.Count == 2);
}

public class Bike
{
    public string Id { get; set; }
    public int Value { get; set; }
    public string Type { get; set; }
}

public class Person
{
    public string Name { get; set; }
    public List<Bike> Bikes { get; set; }
}

To fix this problem a change in the used model is necessary. But what change is needed here to fill the bikes data correctly?

Note: Changing the input document is not an option (as it's a spec)

5
  • 1
    What do you mean by "it's failing"? What's the count? Did the Person object not get a Bikes property populated? Please be explicit in describing what's happening. Commented Jun 11, 2020 at 13:42
  • 3
    Bikes isn't an array, look at Dictionary<string, Bike> structure Commented Jun 11, 2020 at 13:43
  • try this ` public class Bike1 { public int value { get; set; } public string type { get; set; } } public class Bike2 { public int value { get; set; } public string type { get; set; } } public class Bikes { public Bike1 Bike1 { get; set; } public Bike2 Bike2 { get; set; } } public class Example { public string name { get; set; } public Bikes Bikes { get; set; } }` @bertt Commented Jun 11, 2020 at 13:46
  • Can you change the JSON document to have "Bikes" as an array ([ ]) instead of object ({ })? You then also have to move the Id ("Bike1") into the bike object as "id" property Commented Jun 15, 2020 at 8:41
  • @HansKesting changing the input json is not an option in this case (because spec). Commented Jun 15, 2020 at 21:46

1 Answer 1

7

Your code structure is not reflecting your json. Common approach to deserializing json with dynamic property names is to use Dictionary<string, ...> (supported both by Json.NET and System.Text.Json). Try the following:

public class Bike
{
    public int Value { get; set; }
    public string Type { get; set; }
}

public class Person
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Dictionary<string, Bike> Bikes { get; set; }
}

Person.Bikes should be changed to Dictionary<string, Bike> (also Bike.Id property is not needed) cause Bikes json element is not an array but object.

Sign up to request clarification or add additional context in comments.

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.