0

I'm trying to simply serialize a object into json in Unity. I have found several articles on this topic but nothing seems to be working. It is not throwing a exception directly but it is not converting the object to a json string. I have researched the pretty heavily and tried various samples. I'm not sure if the issue is the class or the logic calling the convert to json. I can easily convert using .net but this is for Unity in MonoScript, so the process seems to be a little different. I assume when you convert the object to json string it should not list the base as "null". Its also passing in a empty json string after conversion.

enter image description here

User LogIn Class:

    using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class UserLogIn : MonoBehaviour
{
    public string Email;
    public string Password;
}

Here is my code in unity script:

private UserLogIn _LogIn = new UserLogIn();

    public void SetText(string text)
    {
        //[SerializeField]
        //UserLogIn _LogIn = new UserLogIn();
    //WhiteBoxGamingTestRequest();  
    _LogIn.Email = "testemail@gmail.com";
        _LogIn.Password = "12345";
        string json = JsonUtility.ToJson(_LogIn);
        Debug.Log(json);

        //User_LogIn(_login);

        //text = _bolResponse.ToString();
        //Text txt = transform.Find("Text").GetComponent<Text>();
        //txt.text = text;       
    }

I've tried this: Serialize and Deserialize Json and Json Array in Unity

seems to not work still. Looking for suggestions or corrections.

After conversion json = {} it should be a string containing values. Is Unity's json converter broken?

Conversion: enter image description here

derHugo
  • 83,094
  • 9
  • 75
  • 115
mholmes
  • 177
  • 2
  • 14
  • 1
    Are you using [tag:c#] or [tag:unityscript]? As explained in https://stackoverflow.com/tags/unityscript/info, *Do not use this tag to describe Unity scripts which are in C#: UnityScript is a separate language and should be used only on questions using that language.* – dbc Sep 02 '18 at 23:53
  • What is the problem you are experiencing? You include a screenshot which seems to be of an exception but don't explain the issue other than to say *nothing seems to be working*. If an exception is the problem, please [edit] your question to include the full `ToString()` output of the exception *as text* rather than as a screenshot. For why, please see [Why not upload images of code on SO when asking a question](https://meta.stackoverflow.com/a/285557/3744182) and [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/q/303812/3744182). – dbc Sep 02 '18 at 23:57
  • There is no exception but if you look at the screenshot its not converting to json string – mholmes Sep 03 '18 at 01:36
  • This is a mono script. its not converting the object to a json string. There is no error just fails to convert the object. – mholmes Sep 03 '18 at 01:45

1 Answers1

1

I finally found the answer. In order to make a object in Unity that's convertable to json it appears it needs to be a regular c# object. I'm not sure why you can't use Mono Behavior but that appears to be the problem in my class. So I hope this helps someone else in the future because Unity website was about as clear as mud.

    [System.Serializable]
public class UserLogIn
{
    public string Email;
    public string Password;
}

This is what the json string should look like once converted:

{"Email":"testemail@gmail.com","Password":"12345"}

Also wanted to give credit to this video around 7:43 into it, you can start to see the conversion process https://youtu.be/oJrAT8L4BrA

mholmes
  • 177
  • 2
  • 14
  • Well `MonoBehaviour` is a `component` of a GameObject -> you cannot create one using `new`. Instead you would have to attach it to a GameObject in your scene using `Add`. But since you just want to save pure data without any complex behaviour you always should use a "normal" class for that. – derHugo Sep 03 '18 at 09:47
  • Unity builtins don't serialize nicely. They do clearly serialize, but there's a lot of hidden information about them that Unity uses to serialize that aren't visible to us, as developers. The idea being that we shouldn't serialize gameobjects and components, but rather the data *necessary to respawn them.* – Draco18s no longer trusts SE Sep 03 '18 at 20:31