Unity Serializing object to Json

Unity Serializing object to Json



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





Are you using c# or unityscript? As explained in 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 2 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 and Discourage screenshots of code and/or errors.
– dbc
Sep 2 at 23:57


ToString()





There is no exception but if you look at the screenshot its not converting to json string
– mholmes
Sep 3 at 1: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 3 at 1:45





1 Answer
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





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 3 at 9:47


MonoBehaviour


component


new


Add





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
Sep 3 at 20:31



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?