前提・実現したいこと
UnityでJsonUtility.fromJson()を用いて以下のようなjsonをシリアライズしたく思っています。
が、データ構造はあっているつもりなのになかなかエラーが消えません。
発生している問題・エラーメッセージ
NullReferenceException: A null value was found where an object instance was required.
変換したいjson
{ "status": "ok", "data": { "model": [ { "id": 1, "name": "名前", "path": "http://aaa.com", "created_at": "2018-01-01 00:00:00", "updated_at": "2018-01-01 00:00:00" }, { "id": 2, "name": "名前", "path": "http://bbb.com", "created_at": "2018-01-01 00:00:00", "updated_at": "2018-01-01 00:00:00" } ] } }
該当のソースコード
CSharp
1 [Serializable] 2 public class ServerResponseWith<T> 3 { 4 public string status; 5 public T data; 6 } 7 8 //複数の要素を持つ 9 [Serializable] 10 public class ListData<T> 11 { 12 public List<T> model; 13 } 14 15 16 //modelに含まれるデータ 17 [Serializable] 18 public class ModelsData 19 { 20 public string id; 21 public string name; 22 public string path; 23 public string created_at; 24 public string updated_at; 25 }
CSharp
1Action<UnityWebRequest> serverConnectSuccess = (UnityWebRequest www) => 2 { 3 string downloadedText = www.downloadHandler.text; 4 5 ServerResponseWith<ListData<ModelsData>> serverResponse = JsonUtility.FromJson<ServerResponseWith<ListData<ModelsData>>>(downloadedText); 6 7 if (serverResponse.status == "ok"){ 8 List<ModelsData> models = serverResponse.data.model; 9 } 10 }
試したこと
List<ModelsData>じゃなく、ModelData[]にしたりなど様々なことを試してもnullしか帰って来ず困っています。
JsonUtilityはList<>のネストに対応していないみたいなのも見かけたのですが、この形のjsonはシリアライズできないのでしょうか、ご教授願います。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
Unity 2017.4.1f1 personal
C#
macOS high Sieera
回答4件
あなたの回答
tips
プレビュー