Home > other >  How to parse from JSON to Unity GameObject
How to parse from JSON to Unity GameObject

Time:11-12

I'm building a rudimentary app to store some basic character data. I've gotten as far as storing all of the character data input as serialized data and persistently saved to a JSON file.

Now I'm stuck at trying to figure out how to get that data out of the JSON and into lists. (I figure from there I know how to instantiate new prefab objects populated with the data from the lists, so this is really where I'm stuck.)

My JSON data looks like this (if this helps with providing examples of what to do with it):

"Items": [
        {
            "characterName": "No Name Assigned",
            "chararacterCampaign": "No Campaign Assigned",
            "chararacterJob": "No job Assigned",
            "chararacterTown": "No town Assigned",
            "chararacterRace": "No race Assigned",
            "chararacterSkin": "No skin Assigned",
            "chararacterEyes": "No eyes Assigned",
            "chararacterHair": "No hair Assigned",
            "chararacterAge": "No age Assigned",
            "chararacterBuild": "No build Assigned",
            "chararacterPitch": "no value",
            "chararacterTexture": "no value",
            "chararacterNasality": "no value",
            "chararacterEnunciation": "no value",
            "chararacterTonalRange": "no value",
            "chararacterSpeed": "no value",
            "chararacterVolume": "no value",
            "chararacterAccent": "No accent Assigned"

I have code that is successfully reading the JSON on start, as it isn't overwriting the pre-existing data. The code I've written so far to parse the data looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class CharacterData
{
    public string cName;
    public string campaign;
    public string job;
    public string town;
    public string race;
    public string skin;
    public string eyes;
    public string hair;
    public string age;
    public string build;
    public string pitch;
    public string texture;
    public string nasality;
    public string enunciation;
    public string tonalRange;
    public string speed;
    public string volume;
    public string accent;

    public static CharacterData CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<CharacterData>(jsonString);
    }
}

Issue #1 that I'm running into is that I cannot seem to attach this script to anything, as it does not inherit from MonoBehavior, so right away, I think I need to address that, but I'm not sure how.

Issue #2, where I'm really stuck, is I can't really visualize how to parse that data from the string that will be produced into a format I can then do something with. Any ideas?

CodePudding user response:

You would need to create a class that is a type of MonoBehaviour and add that component on any object in the scene. Here is an example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JSONParser : MonoBehaviour
{
    public CharacterData data;
    void Start()
    {
        data = CharacterData.CreateFromJSON("your json string");
    }
}

enter image description here enter image description here

CodePudding user response:

You cannot deserialize classes inheriting UnityEngine.Object, like ScriptableObject, MonoBehaviour or GameObject. They can only be created by the engine (such as with Instantiate, or a specialized constructor).

Your approach is correct, to deserialize into a class that does not inherit anything. You can then attach that to a variable in another class that is a component. The idea of data-only classes is usually called the "model" or "data transfer object", for this case specifically.

class CharacterController : MonoBehaviour {
    private CharacterData _data;

    private void Awake() {
        _data = CharacterData.CreateFromJSON("your json data");

        // Do something with the data
    }
}
  • Related