public class player{
public string playerId;
public string playerLoc;
}
player[] p = new player[5];
for (int i = 0; i < 5; i++) {
p [i] = new player ();
p [i].playerId = i + "";
p [i].playerLoc = i + "Loc";
}
Debug.Log (JsonUtility.ToJson (p)); //{}
Internally, this method uses the Unity serializer; therefore the type you are creating must be supported by the serializer. It must be a plain class/struct marked with the Serializable attribute.
NOTE: Remove { get; set; } from the player class. If you have { get; set; }, it wont work. Unity's JsonUtility does NOT work with class members that are defined as properties.
Unity3D JsonUtility处理数组
标签(空格分隔): Unity3D
Unity3D自从5.3.3开始提供了JsonUtility的API,用来做JSON相关的处理。
但是使用JsonUtility不能直接处理数组,比如:
StackOverflow,使用了一个巧妙的方法使其支持了数组的转换。我直接使用答主给出的代码,发现还是不能正常使用。
最后在Unity的API说明上找到了原因:
但是按照Unity的API上的代码直接来使用
[Serializable]
,却又发现MonoDevelop报错,是因为[Serializable]
其实是[System.Serializable]
,最后,加上using System;
解决问题。可以,这很JAVA。