Unity中使用Json-1-读取字符串
Unity中使用自带Json模块 - JsonUtility
JsonUtility.FromJson<T>(JSON_STRING);
Unity中使用插件LitJson
JsonMapper.ToObject<T>(JSON_STRING);
使用案例
using UnityEngine;
using LitJson;
public class J_Test : MonoBehaviour
{
string json_str = @"
{
""d"":""d"",
""album"":
{
""name"":""donger"",
""artist"":""Pink"",
""year"":""1973""
}
}
";
public class TestAlbum
{
public string d;
public TestModel album = new TestModel();
}
//JsonUtility 需要序列化 否则转换出来的数值为Null
[System.Serializable]
public class TestModel
{
public string name;
public string artist;
public string year;
}
void Start()
{
Test1();
Test2();
}
void Test1()
{
TestAlbum alb = JsonUtility.FromJson<TestAlbum>(json_str);
print(alb.d);
print(alb.album.artist);
print(alb.album.name);
print(alb.album.year);
}
void Test2()
{
TestAlbum alb = JsonMapper.ToObject<TestAlbum>(json_str);
print(alb.d);
print(alb.album.artist);
print(alb.album.name);
print(alb.album.year);
}
}