Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Accessing JSON class properties properly?

Discussion in 'Scripting' started by SamTyurenkov, Oct 17, 2020.

  1. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    Hi friends,

    I'm struggling with properly working with a JSON from server.

    By now I followed that page to create a Serializable Class:
    https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

    So I have something like that:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [Serializable]
    7. public class SavedGameObject {
    8.  
    9.     public string info;
    10.     public int somevalue;
    11.  
    12.     public static SavedGameObject updateObject(string jsonString)
    13.     {
    14.         return JsonUtility.FromJson<SavedGameObject >(jsonString);
    15.     }
    16. }
    Then, in anoter class I'm using a UnityWebRequest to fetch json from server and trying to update the SavedGameObject with the data from json:

    Code (CSharp):
    1.                
    2.                 yield return www.SendWebRequest();
    3.                 DataBasePlayer.updateObject(www.downloadHandler.text);
    4.                 Debug.Log(DataBasePlayer.info);
    5.  
    But debug log is just null, I'm assuming because the class itself isn't static. However if I try to make it static, that I cannot use FromJson<SavedGameObject >, as the editor raises the error:
    "static types cannot be used as type arguments"

    This might be some basics of coding, though I'm not that good to that level, so any help is much appreciated. Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Not sure what more I can add to this. Make your class and the serializable properties in it NOT static. This is not negotiable.

    You can however make a static factory class that produces class instances from JSON, if that's what you're trying to do.

    Also, I highly recommend NOT using the built-in Unity JSON implementation. It is like a "mini micro tiny pidgin Fisher Price JSON," barely supporting any features, not even supporting common features like Dictionary or HashSet, or properties or pretty much ANYTHING you expect it to support.

    Instead, reach for JSON .NET free from the Asset Store, install it and only use that.

    The hassle you save will be your own. :)
     
    SamTyurenkov likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,446
    Even though JSON.NET is a really greate library, it's quite large. If you're looking for an alternative, I've written the SimpleJSON framework. It is not an object mapper but just a parser which allows easy handling of arbitrary json data without the need of creating extra classes. Especially since webrequests can return variable results (different structured data depending on the result) it's difficult to work with object mappers.

    Assuming your json text looks something like this:

    Code (JavaScript):
    1. {
    2.     "info": "Some info",
    3.     "somevalue": 42
    4. }
    You can simply do this to read the data:

    Code (CSharp):
    1. var node = JSONNode.Parse(www.downloadHandler.text);
    2. string info = node["info"];
    3. int val = node["somevalue"];
     
    SamTyurenkov likes this.
  4. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    Thanks guys, I went for the JSON .NET from the Asset Store, and it made it work as I wanted.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4.  
    5. [Serializable]
    6. public class SavedGameObject : MonoBehaviour
    7. {
    8.  
    9.     public static SavedGameObject savedgame;
    10.  
    11.     public int key1 = 1;
    12.     public int key2 = 1;
    13.  
    14.     private void Awake()
    15.     {
    16.         savedgame = this;
    17.     }
    18.  
    19. }
    Code (CSharp):
    1.  
    2. yield return www.SendWebRequest();
    3. JsonConvert.PopulateObject(www.downloadHandler.text, SavedGameObject.savedgame);
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Really? The DLL is under 400kb... That's smaller than the splash graphic for my game.

    -rw-r--r--  1 kurt  staff  399360 Sep 17 11:21 \
    ./jetpack/Assets/JsonDotNet/Assemblies/AOT/Newtonsoft.Json.dll


    My splash:

    Code (csharp):
    1. -rw-r--r--  1 kurt  staff  412485 Feb 18  2020 20180307_kurt_games_black.png