It is likely that the string returned from webRequest.downloadHandler.text is not the same as the one you posted.
You also don’t need LeaderBoardData leaderBoardData = new LeaderBoardData(); as NewtonsoftJson creates it for you.
I used this script:
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
public class JsonDeserializationTest : MonoBehaviour
{
public string JsonData;
private void Start()
{
var leaderBoardData = JsonConvert.DeserializeObject<LeaderBoardData>(JsonData);
leaderBoardData.leaders?.ForEach(x => Debug.Log(x.name));
}
}
public class Leader
{
public string name { get; set; }
}
public class LeaderBoardData
{
public List<Leader> leaders { get; set; }
}
I just copy pasted the
into the JsonData field in the inspector. It works just fine, all names are being logged in the console.
NewtonsoftJson can deserialize properties. Unity’s JsonUtility cannot.
List can be deserialized just fine. You don’t need an array necessarily.
Unity’s JsonUtility is very limited which is why people tend to use NewtonsoftJson instead.
Indeed!! It’s great if you only want the features it can do, but last I checked it falls flat on List() as well as many other common generic collections.
Here’s some more blurbiage:
Problems with Unity “tiny lite” built-in JSON:
In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.
Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).
I don’t think you’re making much sense. I have been using JsonUtility for most of my game’s string and numeric data, and I haven’t had one single issue with the utility, never experienced one fail. And I am doing stuff far more complicated than here.
JSON only supports a very restricted set of data types. It doesn’t support “dictionaries” or “hashes” or “properties”, and if you start doing stuff like that, the end result is unspecified.
Just use your tools right and set your entity objects to match these data types and it will work. And don’t use JSON for anything else than the data it supports.
Please refrain from typing in caps or bold when talking with me, it is against basic netiquette. I have to say also that the advice you give to people on this forum often comprises of (sometimes bolded) generic statements that don’t necessarily address the issue at hand and respond to the person. Just I know that I personally don’t like being shouted at with comments that don’t address the content of what I am saying.
Secondly, my comment that you are not making much sense wasn’t directed at any possibly infactual statement you made, but that the implied reason behind what you are doing is not clear to me. Which I elaborated afterwards and should be clear from the context.
To answer the specific issue here, I don’t think the issue is that @Kastalche is using a wrong library, after all, what kind of JSON library can’t handle an array of string properties? The issue might be that he is using properties, which are not supported by JSON, hence he is using the library incorrectly.
So, if you know that his issue was that he is using properties, why not say so?
Well, what doesn’t make sense is that the OP was already using Json.NET and you just ranted again about Unity’s JsonUtility for no reason
I guess the whole world already knows your stance on the JsonUtility and while your view has some valid points, it’s certainly not the only valid view. Yes, the JsonUtility does have limitations but also many advantages. It’s a lot faster, doesn’t allocate as much garbage, is pretty much in line with Unity’s built-in serialization and is already shipped with Unity and therefore lightweight.
Even Json.NET has it’s limitations and requires workarounds. Yes, Newtonsoft’s powerhouse has options for almost any usecase, but you still need to be aware of those gotchas. People have tried deserializing a scriptableobject with Json.NET and were equally confused why it doesn’t work.
So “the right tool for the job” should be applied more often. For most things related to json I use my own SimpleJSON most of the time because I barely actually need the data in actual objects.
I would also like to add that a great workman doesn’t blame his tools. Tools might have absolute restrictions that make them unusable for the job, but rather for a true techie the challenge is how to go to the Moon with your old VIC-20.
That’s on me. I read the code and incorrectly read it as Unity’s JSON, given that most folks posting with JSON errors tend to have started out using that.