Read JsonObject Data

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).

[
  {
    "Condition": ["Friendship"],
    "Dialogue": "a dialogue"
  },
  {
    "Condition": ["Neutral", "Enmity"],
    "Dialogue": "a dialogue",
    "Characters": [11, 26]
  },
  {
    "Condition": ["Neutral", "Friendship"],
    "Dialogue": "a dialogue",
    "Characters": [11, 26]
  }
]

Does anyone knows how can I read, store and use this data?. I already tried doing this

var jsonResponse = File.ReadAllText(Path.Combine(""));
Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);

public class Root
{
    public List<string> Condition { get; set; }
    public string Dialogue { get; set; }
    public List<int> Characters { get; set; }
}

But it says:
Cannot implicitly convert type ‘System.Collections.Generic.List<ReadDialogueJson.Root>’ to ‘ReadDialogueJson.Root’

As I understand, I need to read the Json as a text and then parse that Text, Right?. But it’s not working. Can anyone help me?.
Thanks in advance.

Are you asking for links to a JSON tutorial??

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).

https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

Also, always be sure to leverage sites like:

https://jsonlint.com
https://json2csharp.com
https://csharp2json.io

If the above doesn’t answer your question then I have no idea what you are asking.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)
2 Likes

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.

1 Like

JSON can be banged on any number of ways.

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.

Look at this line:

Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);

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.

So just do this

List<Root> yourData = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);

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

2 Likes

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.

1 Like

Welp, I’m finally able to access and utilize the data of the Json. Awesome!.

Thank you all for your help.

1 Like