[英]Unity Serializing object to Json
I'm trying to simply serialize a object into json in Unity. 我试图将对象简单地序列化为Unity中的json。 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.
它不会直接引发异常,但不会将对象转换为json字符串。 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.
我不确定问题是类还是调用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.
我可以轻松地使用.net进行转换,但这是针对MonoScript中的Unity的,因此该过程似乎有些不同。 I assume when you convert the object to json string it should not list the base as "null".
我假设当您将对象转换为json字符串时,它不应将基准列为“ null”。 Its also passing in a empty json string after conversion.
转换后还传递一个空的json字符串。
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 我已经尝试过: 在Unity中序列化和反序列化Json和Json Array
seems to not work still. 似乎仍然无法正常工作。 Looking for suggestions or corrections.
寻找建议或更正。
After conversion json = {} it should be a string containing values. 转换后的json = {},它应该是一个包含值的字符串。 Is Unity's json converter broken?
Unity的json转换器损坏了吗?
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.
为了在Unity中制作一个可转换为json的对象,它看起来必须是常规的c#对象。 I'm not sure why you can't use Mono Behavior but that appears to be the problem in my class.
我不确定为什么不能使用Mono Behavior,但是这似乎是我班上的问题。 So I hope this helps someone else in the future because Unity website was about as clear as mud.
因此,我希望这对以后的人有所帮助,因为Unity网站简直就像泥泞一样。
[System.Serializable]
public class UserLogIn
{
public string Email;
public string Password;
}
This is what the json string should look like once converted: 转换后的json字符串应如下所示:
{"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 也想在这部影片的7:43左右获得赞誉,您可以开始查看转换过程https://youtu.be/oJrAT8L4BrA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.