Hi, I have to read the info from various Jsons that have this format:
(These are dummy keys and values, but the format is exactly the same as the ones I’m trying to read and use).
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 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’m actually saying that I have no idea on how to read the data structure of my JsonObject, as all the tutorials I have found are for different DataStructures than the one I have to work with.
I don’t know the steps to read the data and put it inside variables/attributes. (I’m new at c#).
EDIT:
I’ve read what you told me and I’m now testing code with the sites you kindly provided.
I think this will help me to make it work, thank you.
Making a properly-shaped class and deserializing into it is the most straightforward way.
Otherwise you can write your own crazy parsing code to consume it on the fly. I don’t recommend this unless you a) understand why you need to, and b) know what you are doing with the data that requires it.
I still don’t seem to manage create a script that I can run.
I’ve validated the Json with the site “jsonlint” and I’ve use the site “json2csharp” to get the code that allows me to read the json. This:
// Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);
public class Root
{
public List<string> Condition { get; set; }
public string Dialogue { get; set; }
public List<int> Characters { get; set; }
}
But what I’m supposed to do with that?. I don’t seem to find any example that would help me. I’ve been stuck trying to make this work for weeks now.
var jsonResponse = File.ReadAllText(Path.Combine("part of the path", "part of the path", "etc"));
Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);
It’s throwing error at the “jsonResponse”, it says:
“Cannot implicitly convert type ‘System.Collections.Generic.List<ReadDialogueJson.Root>’ to 'ReadDialogueJson.Root”.
I’m supposed to read the text from the Json and then deserialize it, Right?. How do I do that?, I can’t find it anywhere.
You tell the DeserializeObject method to return a List<Root> but you declared your “myDeserializedClass” as type Root. The error tells you that it can not assing or convert a List<Root> (that is returned) to a variable of type Root.
Note that your json data does not have a root object, but has an array as the root element. That’s why you read in a List instead of a single object. You do understand your own json data, right? If not have a look at json.org which has a great overview what json can represent and what the syntax actually means. It’s very simple
First, thanks, that solved the Issue.
Second, yes I understand my Json, I have a JArray in which each position is a {} element with keys that have values…
The problem was that I didn’t know how to put it in c# code (because I’m not used to strong typed languages).
I will try to run it now and see if I can access and use the data properly. Thank you.