Unity initializes JSON List to zero

Hey all!

I have a quick question about an issue I’m having about JSON deserialization.

Before, you say anything, yes, I know Newtonsoft is better. As of right now, I just need something lightweight.

I have a JSON file that looks like this:

{
    "names": {
        "firstNames": [
            "Abigail", "Adam", "Adrian", "Aiden", "Alexander", "Alice", 
            "Amelia", "Andrew", "Anna", "Aria", "Ariana", "Asher", 
            "Benjamin", "Bella", "Blake", "Brianna", "Brooklyn", 
            "Caleb", "Cameron", "Charlotte", "Claire", "Connor", 
            "Daniel", "David", "Dylan", 
            "Eleanor", "Elizabeth", "Ella", "Emily", "Emma", "Ethan", 
            "Evelyn", 
            "Faith", "Finn", "Franklin", 
            "Gabriel", "Grace", 
            "Hannah", "Harper", "Henry", 
            "Isaac", "Isabella", "Isaiah", 
            "Jack", "Jacob", "James", "Jason", "John", "Joseph", "Joshua", 
            "Julia", "Julian", "Justin", 
            "Katherine", "Kevin", "Kylie", 
            "Liam", "Lily", "Lucas", "Lucy", 
            "Madison", "Mason", "Matthew", "Michael", "Mia", 
            "Nathan", "Nicole", "Noah", 
            "Oliver", "Olivia", "Owen", 
            "Penelope", "Peter", 
            "Quinn", 
            "Rachel", "Rebecca", "Riley", "Ryan", 
            "Samantha", "Samuel", "Sarah", "Sophia", "Sophie", "Steven", 
            "Thomas", "Tyler", 
            "Victoria", 
            "William", 
            "Zachary", "Zoe"
        ],
        "lastNames": [
            "Adams", "Alexander", "Allen", "Anderson", "Andrews", 
            "Bailey", "Baker", "Barnes", "Bell", "Bennett", "Brown", 
            "Campbell", "Carter", "Chapman", "Clark", "Collins", "Cooper", 
            "Davis", "Diaz", "Dixon", 
            "Edwards", "Ellis", "Evans", 
            "Ferguson", "Fisher", "Foster", "Freeman", 
            "Garcia", "Green", "Griffin", 
            "Hamilton", "Harris", "Harrison", "Hart", "Harvey", "Henderson", 
            "Hill", "Holmes", "Howard", 
            "Jackson", "James", "Jenkins", "Johnson", "Jones", 
            "Kelly", "Kennedy", "Kim", "King", "Kumar", 
            "Lee", "Lewis", "Lopez", 
            "Martinez", "Miller", "Mitchell", "Moore", "Morgan", 
            "Nelson", "Parker", "Patterson", "Patel", "Peterson", "Phillips", 
            "Ramirez", "Reed", "Richardson", "Rivera", "Roberts", "Robinson", 
            "Rodriguez", "Rogers", 
            "Sanders", "Scott", "Smith", "Stevens", 
            "Taylor", "Thomas", "Thompson", 
            "Walker", "Washington", "White", "Williams", "Wilson", "Wood", 
            "Young"
        ]
    }
}

My object looks like this:

    [System.Serializable]
    public class Names
    {
        public List<string> firstNames;
        public List<string> lastNames;
    }

And deserilizing the JSON file looks like this:

   Names names;
   private void Start()
   {
        names = new Names();
        names = JsonUtility.FromJson<Names>(nameFile.text);
   }

However, each list gets a count of zero after the JSON file is deserialized.

Any thoughts why this could be?

I think you’re gonna need a root object with that Names class in it.

That’s how json2csharp suggests it anyway…

Then you deserialize it by using that outer container type supplied to the FromJson<OuterContainer>() call.

1 Like

You can remove the “names” object from the json and add the “firstNames” and “lastNames” directly to the root object.

Btw a small tip, when I work with json in C# I always create a test object in my code and let the json class write it to a string or file, then I can see what kind of json format it actuall writes and reads later. Then I can compare it with my existing json files and either adjust my json file or the c# code until both match and understand the same format.

2 Likes

To add to the good advice above, in here:

The new Names(); is unecessary as JsonUtility.FromJson is making a new instance anyway.

2 Likes