There are 4 known possible reasons why you may get empty Json in Unity.
1.Not including [Serializable]. You get empty json if you don't include this.
2.Using property (get/set) as your variable. JsonUtility does not support this.
3.Trying to serializing a collection other than List.
4.Your json is multi array which JsonUtility does not support and needs a wrapper to work.
The problem looks like #1. You are missing [Serializable] on the the classes. You must add using System; in order to use that.
Copy[Serializable]
public class SpriteData {
public string sprite_name;
public Vector2 sprite_size;
public List<Vector2> subimage;
}
and
Copy[Serializable]
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
5.Like the example, given above in the SpriteData class, the variable must be a public variable. If it is a private variable, add [SerializeField] at the top of it.
Copy[Serializable]
public class SpriteDataCollection
{
[SerializeField]
private SpriteData[] sprites;
}
If still not working then your json is probably invalid. Read "4.TROUBLESHOOTING JsonUtility" from the answer in the "Serialize and Deserialize Json and Json Array in Unity" post. That should give you inside on how to fix this.
Answer from Programmer on Stack OverflowThere are 4 known possible reasons why you may get empty Json in Unity.
1.Not including [Serializable]. You get empty json if you don't include this.
2.Using property (get/set) as your variable. JsonUtility does not support this.
3.Trying to serializing a collection other than List.
4.Your json is multi array which JsonUtility does not support and needs a wrapper to work.
The problem looks like #1. You are missing [Serializable] on the the classes. You must add using System; in order to use that.
Copy[Serializable]
public class SpriteData {
public string sprite_name;
public Vector2 sprite_size;
public List<Vector2> subimage;
}
and
Copy[Serializable]
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
5.Like the example, given above in the SpriteData class, the variable must be a public variable. If it is a private variable, add [SerializeField] at the top of it.
Copy[Serializable]
public class SpriteDataCollection
{
[SerializeField]
private SpriteData[] sprites;
}
If still not working then your json is probably invalid. Read "4.TROUBLESHOOTING JsonUtility" from the answer in the "Serialize and Deserialize Json and Json Array in Unity" post. That should give you inside on how to fix this.
I know this post is old.. However, I recently had a similar problem to the question here. To clarify...
Using JsonUtility, if you have a List of a "serializable" class, JsonUtility will return an Empty response.
Copy[Serializable]
public class SomeSerializableClass
{
public SomeSerializableClassB instance = new();
}
[Serializable]
public class SomeSerializableClassB
{
public int x = 123;
}
public class Something{
public List<SomeSerializableClass> collection = new();
public string GetJson()
{
return JsonUtility.ToJson(collection);
}
}
GetJson() here will return nothing. Instead, wrap the List collection into another class, then it will serialize and convert correctly.
Copy[Serializable]
public class SomeSerializableClassList
{
public List<SomeSerializableClass> instances = new();
}
[Serializable]
public class SomeSerializableClass
{
public SomeSerializableClassB instance = new();
}
[Serializable]
public class SomeSerializableClassB
{
public int x = 123;
}
public class Something{
public SomeSerializableClassList collection = new();
public string GetJson()
{
return JsonUtility.ToJson(collection);
}
}
Its difficult to comprehend why this doesn't work when you read "Lists are supported". Maybe someone smarter than me can explain...
[SOLVED] Converting a Generic List to JSON in Unity - Questions & Answers - Unity Discussions
List array to json file / makes objects with parsed json file
JsonUtilities.ToJson with List not working as expected - Unity Engine - Unity Discussions
unity - How can I serialize lists to JSON on mobile? - Game Development Stack Exchange
Videos
hi guys.
I'm korean college student.
I'm sorry about my dirty codes to see you.
I have problem with unity.
I want changing my List array to json file.
And parsing json file, and create objects with that.
So, I write some code, and run it.
but doesn't make json file.
---------------------------
string json = JsonUtility.ToJson(AllLectureList);
Debug.Log(json);
--------------------------
console only say
-------------------
{}
unityEngine.Debug.log(object)
there are belows my whole code
<LecDatabase.cs>
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using System.IO;
[System.Serializable]
public class LecDatabase : MonoBehaviour
{
public static LecDatabase lecDatabase;
public int Credit;
private void Awake()
{
lecDatabase = this;
}
public List<Lecture> LectureDB = new List<Lecture>();
public GameObject LecItemPrefab;
public Vector3[] pos;
private void Start()
{
for (int i= 0; i<4; i++)
{
for(int j=0; j<12; j++)
{
LectureDB[(i * 12 + j)].LecName = "major" + ((i * 12) + (j + 1));
LectureDB[i * 12 + j].LecYear = i + 1;
LectureDB[i * 12 + j].lecType = LecType.Major;
LectureDB[i * 12 + j].IsSignUp = false;
LectureDB[i * 12 + j].LecCredit = 3;
}
}
for(int i=0; i<5; i++)
{
LectureDB[48 + i].LecName = "PreRequisite" + (48+i);
LectureDB[48 + i].LecYear = 1;
LectureDB[48 + i].lecType = LecType.PreRequisite;
LectureDB[48 + i].IsSignUp = false;
LectureDB[48 + i].LecCredit = 2;
}
for (int i = 0; i < 30; i++)
{
LectureDB[53 + i].LecName = "Elective" + (53+i);
LectureDB[53 + i].LecYear = 1;
LectureDB[53 + i].lecType = LecType.Elective;
LectureDB[53 + i].IsSignUp = false;
LectureDB[53 + i].LecCredit = 2;
}
List<Lecture> AllLectureList = LectureDB;
string json = JsonUtility.ToJson(AllLectureList);
Debug.Log(json);
Credit = 0;
}
}
<Lectures.cs>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public enum LecType
{
Major,
PreRequisite,
Elective
}
[System.Serializable]
public class Lecture
{
public LecType lecType;
public string LecName;
public int LecCredit;
public int LecYear;
public bool IsSignUp;
}
To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is.
Something like this should work. You can make some sort of generic class like here, if you only need to serialize lists containing different types of data:
using System.Collections.Generic;
using UnityEngine;
public class SerializationDemo : MonoBehaviour
{
[SerializeField] List<string> yourList;
void Start()
{
// Your list with some data
yourList = new List<string> { "foo", "bar" };
// Store your data into a container class,
// this can be serialized
var data = new SaveData<string>(yourList);
// Class contents to json using Unity's serializer
var jsonText = JsonUtility.ToJson(data);
// Save your json data here instead of logging it...
// JSON: {"list":["foo","bar"]}
Debug.Log("JSON: " + jsonText.ToString());
}
}
[System.Serializable]
public class SaveData<T>
{
public List<T> list;
public SaveData(List<T> data)
{
this.list = data;
}
}
The JSON.NET plugin for Unity officially supports "all Unity platforms including WebGL, except for WebPlayer, Windows 8.0 and Windows Phone 8.0."
Upto now I've been using the older 5.3 way of converting a List of objects into a very, very long string and serializing that. But I've recently found out the builtin JsonUtility can now work with arrays?
I'm new to this but I'm struggling to do this as simple as possible ;0
For the record I'm trying to create a 3d tile loading and saving system so its a bunch of world xyz etc and Tiletype.