- Home /
Object to JSON Conversion
I want to convert the object into JSON string but when I try to convert using JSON utility it returns the empty string. Below is the code
using UnityEngine;
using System;
public sealed class Player : MonoBehaviour
{
#region VARIABLES
//Public Variables
public static Player Instance { get; private set; }
internal BibleVersions bibleVersion = BibleVersions.Web;
internal Gender gender = Gender.Female;
internal int experience = 0, requiredExperience = 0, level = 1, gold = 100, lastBonusTime = 0, lastEmptyBonusTime = 0, totalSavedCards = 1, totalSavedVerses = 1;
internal int bookTextureIndex = 0, shieldTextureIndex = 0, hairTextureIndex = 0, topTextureIndex = 0, bottomTextureIndex = 0, bootTextureIndex = 0, gloveTextureIndex = 0, helmetTextureIndex = 0, skinIndex = 0;
internal string bookID = string.Empty, shieldID = string.Empty, hairID = string.Empty, topID = string.Empty, bottomID = string.Empty, bootID = string.Empty, gloveID = string.Empty, helmetID = String.Empty;
internal string joinedDate = string.Empty, lastActiveDate = string.Empty, premiumDate = String.Empty;
internal bool ghostBan = false, disableUser = false;
internal float averageDPS = 3.0f, huntDPS = 0.0f;
#endregion
void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
// Use this for initialization
void Start()
{
Debug.Log(Str());
}
// Update is called once per frame
void Update()
{
}
public string Str()
{
Player player1 = new Player();
player1.bibleVersion = SaveGameData.Instance.bibleVersion;
player1.experience = SaveGameData.Instance.experience;
player1.level = SaveGameData.Instance.level;
player1.gold = SaveGameData.Instance.gold;
player1.lastBonusTime = SaveGameData.Instance.lastBonusTime;
player1.lastEmptyBonusTime = SaveGameData.Instance.lastEmptyBonusTime;
player1.totalSavedCards = SaveGameData.Instance.totalSavedCards;
player1.totalSavedVerses = SaveGameData.Instance.totalSavedVerses;
player1.bookTextureIndex = SaveGameData.Instance.bookTextureIndex;
player1.shieldTextureIndex = SaveGameData.Instance.shieldTextureIndex;
player1.hairTextureIndex = SaveGameData.Instance.hairTextureIndex;
player1.topTextureIndex = SaveGameData.Instance.topTextureIndex;
player1.bottomTextureIndex = SaveGameData.Instance.bottomTextureIndex;
player1.bootTextureIndex = SaveGameData.Instance.bootTextureIndex;
player1.gloveTextureIndex = SaveGameData.Instance.gloveTextureIndex;
player1.bookID = SaveGameData.Instance.bookID;
player1.shieldID = SaveGameData.Instance.shieldID;
player1.hairID = SaveGameData.Instance.hairID;
player1.topID = SaveGameData.Instance.topID;
player1.bottomID = SaveGameData.Instance.bottomID;
player1.bootID = SaveGameData.Instance.bootID;
player1.gloveID = SaveGameData.Instance.gloveID;
player1.joinedDate = SaveGameData.Instance.joinedDate;
player1.lastActiveDate = SaveGameData.Instance.lastActiveDate;
player1.requiredExperience = SaveGameData.Instance.requiredExperience;
player1.premiumDate = SaveGameData.Instance.premiumDate;
player1.ghostBan = SaveGameData.Instance.ghostBan;
player1.disableUser = SaveGameData.Instance.disableUser;
player1.helmetID = SaveGameData.Instance.helmetID;
player1.helmetTextureIndex = SaveGameData.Instance.helmetTextureIndex;
player1.skinIndex = SaveGameData.Instance.skinIndex;
player1.averageDPS = SaveGameData.Instance.averageDPS;
player1.huntDPS = SaveGameData.Instance.huntDPS;
string str = JsonUtility.ToJson(this);
Debug.Log("str=" + str);
return str;
}
}
Am I missing something here? I have worked with JSON before but it's the first time that it's happening to me. Any help would be appreciated. THanks!
Answer by Huacanacha · Mar 04, 2019 at 09:20 PM
Per the docs ToJson will:
Generate a JSON representation of the public fields of an object.
You don't have any public fields on your Player class.
The builtin JsonUtility is convenient but not very flexible. You may be better served looking at a third party JSON library.
Yes you are right. After some searching I found something similar to my post on stackoverflow
it seems that builtin json can only generate public members. For third party JSON lib I will recommend Newtonsoft JSON
Answer by Cornelis-de-Jager · Mar 04, 2019 at 09:00 PM
Checkout the Tutorial from the Unity Page: HERE