1

I have a JSON-file people.json containing multiple objects that I want to parse to a list People containing Person objects but with no luck. I get null from peopleLis and I suspect that I do something (probably a lot) wrong here. Can you guys help me out?

{
    "Andrew": {
        "weight": "75",
        "height": "181"
    },
    "Nathalie": {
        "weight": "68",
        "height": "182"
    },
    "Dave": {
        "weight": "83",
        "height": "192"
    }
}

This is the code I tried out:

        public class Person
        {
            public int weight { get; set; }
            public int height { get; set; }
        }

        public class People
        {
            public List<Person> people { get; set; }
        }

        static void Main(string[] args)
        {
            People peopleList = JsonConvert.DeserializeObject<People>(File.ReadAllText(@"C:\people.json"));
        }
2
  • As a clue to what the problem is, you can put your JSON into this page, and see the classes that are generated. Commented Mar 18, 2021 at 12:18
  • The json should start with [ and end with ] for this to be a list of objects. Currently you have {, }.. Commented Mar 18, 2021 at 12:20

2 Answers 2

6

Your JSON doesn't represent a list of people - it represents a single object with multiple properties, where each property value is a person.

You can deserialize this to a Dictionary<string, Person>:

string json = File.ReadAllText(@"C:\people.json");
var people = JsonConvert.DeserializeObject<Dictionary<string, Person>>(json);

You can create a list from that if you want (e.g. people.Values.ToList()) but you shouldn't depend on the ordering of that list being the same as in the JSON file - fundamentally, JSON object properties aren't intended to be order-sensitive. Given that the name of the person is presumably important, I'd stick with using the dictionary, where the key of each entry is the name.

(As a side-note, I'd recommend using idiomatic names for the properties, so Weight and Height, then using [JsonProperty] to specify the JSON names if you need to.)

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

Comments

0

There are a few things not working out here:

  1. As mentioned above the JSON does not represent a List structure, it's really 3 different objects named Andrew, Nathalie and Dave
  2. The above class "People" also has to be represented in the JSON - right now it's not.
  3. Maybe there is a need for another field called "Name" just like "height" and "weight"
  4. For the classes to store the values (I suspect you need that:)you will need member variables and not only the properties function

I don't know if you can change the JSON or/and the code but here is a piece of code and a JSON that might work for you - I did not try it out but you I think you get the picture

public class Person
{
    private string _name;
    private int _weight;
    private int _height;


    public string Name { get => _name; set => _name = value; }
    public int Height { get => _height; set => _height = value; }

    public int Weight { get => _weight; set => _weight = value; }
}

public class PeopleList
{
    private List<Person> people;

    public List<Person> People { get => people; set => people = value; }
}

static void Main(string[] args)
{
    PeopleList peopleList = JsonConvert.DeserializeObject<People>(File.ReadAllText(@"C:\people.json"));
}

And this is the JSON

{
  "People": [
    {
      "Name": "Andrew",
      "Weight": "75",
      "Height": "181"
    },
    {
      "Name": "Nathalie",
      "Weight": "68",
      "Height": "182"
    },
    {
      "Name": "Dave",
      "Weight": "83",
      "Height": "192"
    }
  ]
}

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.