facebook twitter hatena line email

「Unity/Csharp/Json」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(json展開)
(json展開)
行4: 行4:
 
==json展開==
 
==json展開==
 
  public class AuthScript : MonoBehaviour {
 
  public class AuthScript : MonoBehaviour {
class ResData {
+
    class ResData {
public string status = "ok";
+
        public string status = "ok";
public string notice = "";
+
        public string notice = "";
public string user;
+
        public string user;
 +
    }
 +
    class ResUser {
 +
        public int id = 0;
 +
        public string name = "";
 
  }
 
  }
class ResUser {
+
    void ExecJsonParse (string json) {
public int id = 0;
+
public string name = "";
+
}
+
      void ExecJsonParse (string json) {
+
 
         ResData resData = JsonUtility.FromJson<ResData>(json);
 
         ResData resData = JsonUtility.FromJson<ResData>(json);
 
         Debug.Log("status=" + resData.status);
 
         Debug.Log("status=" + resData.status);
 
         Debug.Log("notice=" + resData.notice);
 
         Debug.Log("notice=" + resData.notice);
      }
+
    }
 
  }
 
  }
  
 
==公式JsonUtility==
 
==公式JsonUtility==
 
https://docs.unity3d.com/ScriptReference/JsonUtility.html
 
https://docs.unity3d.com/ScriptReference/JsonUtility.html

2017年11月9日 (木) 11:44時点における版

jsonを扱うには

Unity 5.3からJsonUtilityが使えるようになったので、JsonUtilityを使った

json展開

public class AuthScript : MonoBehaviour {
    class ResData {
        public string status = "ok";
        public string notice = "";
        public string user;
    }
    class ResUser {
        public int id = 0;
        public string name = "";
	}
    void ExecJsonParse (string json) {
       ResData resData = JsonUtility.FromJson<ResData>(json);
       Debug.Log("status=" + resData.status);
       Debug.Log("notice=" + resData.notice);
    }
}

公式JsonUtility

https://docs.unity3d.com/ScriptReference/JsonUtility.html