本文介绍了Unity C#JsonUtility未序列化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些需要序列化/反序列化的数据,但是JsonUtility并未按预期进行.这是我正在使用的对象:

I've got some data I need to serialize/deserialize, but JsonUtility is just not doing what it's supposed to. Here's the objects I'm working with:

public class SpriteData {
    public string sprite_name;
    public Vector2 sprite_size;
    public List<Vector2> subimage;
}

public class SpriteDataCollection
{
    public SpriteData[] sprites;
}

如果我创建一个SpriteDataCollection,并尝试使用JsonUtility对其进行序列化,那么我只会得到一个空对象{}.这是它的构建方式:

If I create a SpriteDataCollection, and attempt to serialize it with JsonUtility, I just get an empty object {}. Here's how it's being built:

            SpriteData data = new SpriteData();
            data.sprite_name = "idle";
            data.sprite_size = new Vector2(64.0f, 64.0f);
            data.subimage = new List<Vector2> { new Vector2(0.0f, 0.0f) };

            SpriteDataCollection col = new SpriteDataCollection();
            col.sprites = new SpriteData[] { data };

            Debug.Log(JsonUtility.ToJson(col));

调试日志仅显示"{}".为什么不序列化任何内容?我已经对其进行了测试,对单个SpriteData进行序列化完全可以实现它的预期功能,但是在SpriteDataCollection中将无法正常工作.

The debug log only prints "{}". Why isn't it serializing anything? I've tested it out, and serializing a single SpriteData does exactly what it's supposed to do, but it won't work in the SpriteDataCollection.

推荐答案

在Unity中获得空Json的已知原因有四个.

There are 4 known possible reasons why you may get empty Json in Unity.

1 .不包括[Serializable].如果不包含此内容,则会得到空的json.

1.Not including [Serializable]. You get empty json if you don't include this.

2 .使用属性(获取/设置)作为变量. JsonUtility不支持此功能.

2.Using property (get/set) as your variable. JsonUtility does not support this.

3 .尝试序列化List以外的集合.

3.Trying to serializing a collection other than List.

4 .您的json是JsonUtility不支持的多数组,需要包装器工作.

4.Your json is multi array which JsonUtility does not support and needs a wrapper to work.

问题看起来像#1 .您在课程上缺少[Serializable].您必须添加using System;才能使用它.

The problem looks like #1. You are missing [Serializable] on the the classes. You must add using System; in order to use that.

[Serializable]
public class SpriteData {
    public string sprite_name;
    public Vector2 sprite_size;
    public List<Vector2> subimage;
}

[Serializable]
public class SpriteDataCollection
{
    public SpriteData[] sprites;
}

5 .就像上面在SpriteData类中给出的示例一样,变量必须是公共变量.如果它是一个私有变量,请在其顶部添加[SerializeField].

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.

[Serializable]
public class SpriteDataCollection
{
    [SerializeField]
    private SpriteData[] sprites;
}


如果仍然无法正常工作,则您的json可能无效.从.那应该让您了解如何解决此问题.


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.

这篇关于Unity C#JsonUtility未序列化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:52