C# problem with JSONUtility

Hello guys, I have a probblem with deserializing a json file.

My JSON file: {"Texts":[ { "text": "The big story is the unmasking and @surveill - Pastebin.com

Following this guide: c# - Serialize and Deserialize Json and Json Array in Unity - Stack Overflow

I made this code: using System.Collections;using System.Collections.Generic;using UnityEngine; - Pastebin.com

The fact is that fromjson returns an empty object (even if my json file is not empty and is well formatted according to https://jsonformatter.curiousconcept.com/) (I commented the code so that you can see where is the problem).

I don’t really know what to do because i’m very new to json and jsonutility, so what you suggest me? What are your solutions to this?

Thank you in advance!

I’m actually very confused why you created a wrapper class…However, I’m guessing what you are casting it to isn’t correct. If you toss your json into this site http://json2csharp.com/ you will see that you should have two classes. Now, jsonutility uses fields instead of properties, so you should be able to make the properties fields and make sure your classes are Serializable (which you did) and you’re good to go.

RootObject is the class you’ll cast into. You can name that class whatever you want. They just put RootObject to show it’s the top class.

Thank you for your reply! I did that because I read that it’s impossibile with jsonutility to work with json arrays (as the guy in that stackoverflow.com link said)

I tried to do what you said, but again it’s not working: NULLREFERENCEEXCEPTION.

What I tried so far: instead of the serializable class that I created before, I pasted the two classes generated by json2csharp (without get and set) inside my code and I made them serializable. Now if I try to do tweetText.Texts[0].text after the declaration of tweetText for example, it continues to be null.

I’m guessing jsonUtility is going to turn it into a list (I use json.net instead of jsonutility, but I think they turn collections into list). Repost your updated code here (just use code tags) and I’ll see if something is off on it. Include the new json classes as well.

ok so I tried to delete {get,set} from the variables inside the two new classes and i don’t have NULLREFERENCEEXCEPTION anymore, but now I have a new error ArgumentOutOfRangeException.

This is My Class MoveTweetDown: [System.Serializable]public class TextOfTweet{ public string text;} - Pastebin.com

JSON is the same as the first post.

The error is:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[TextOfTweet].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
MoveTweetDown.OnEnable () (at Assets/_Script/MoveTweetDown.cs:64)
UnityEngine.GameObject:SetActive(Boolean)
LevelManager:Update() (at Assets/_Script/LevelManager.cs:40)

It Appears everytime the attached gameObject is enabled (of course).

Where you read LevelManager:Update() (at Assets/_Script/LevelManager.cs:40), that line is here: Public Class LevelManager: MonoBehaviour{ ...... GameObject tweet = Twee - Pastebin.com

Just means the list is empty. But I can’t tell you exactly why yet, unless your json string is empty. I would debug the jsonstring to make sure you are getting something there correctly. I can look a bit deeper later, just can’t right now.

jsonString returns exactly my json file structure: https://pastebin.com/3qUF65mr

public List tweetTexts;

try changing this to

public List Texts;

1 Like

dude… just… Why it worked? LOL

Good solution, but can you explain me why it worked?

EDIT: ah ok, it’s because the json begins with Texts. THANK YOU! :smile:

1 Like

Yeah, I am having a busy work day and overlooked the json, but the json returns Texts, so the variable name has to match, which you just figured out as well. Glad it’s working for you!

1 Like