官網案例傳送門
我這裡不過是借花獻佛,案例官網就有.
1 using UnityEngine; 2 using System.Collections; 3 4 public class json : MonoBehaviour { 5 6 public class JsonInfo 7 { 8 public int id; 9 public string name; 10 public int age; 11 } 12 13 void Start () { 14 JsonInfo Info=new JsonInfo(); 15 Info.id = 0; 16 Info.name = "WenanLee"; 17 Info.age = 15; 18 19 print(ToJson(Info)); 20 21 print(FromJson(ToJson(Info)).name); 22 } 23 24 string ToJson(JsonInfo info) 25 { 26 return JsonUtility.ToJson(info); 27 } 28 29 JsonInfo FromJson(string jsonStr) 30 { 31 return JsonUtility.FromJson<JsonInfo>(jsonStr); 32 } 33 }