jsonutilityシリアル化と砂漠化JSONとJSONアレイは、統一で使用されます。
質問回答 Unity 5.3.3のバージョンから始めて、JSONUtilityを使用してJSONデータを処理することをお勧めします。これは、高性能で使いやすいためです。
出力:
大文字:
Player playerInstance = new Player();
playerInstance.playerId = "8484239823";
playerInstance.playerLoc = "Powai";
playerInstance.playerNick = "Random Nick";
string playerToJson = JsonUtility.ToJson(playerInstance);
JSONアレイを処理するには、このGitHub倉庫の補助クラスを使用できます。
Auxiliary class -jsonhelper.cs
<code class="language-json">{"playerId":"8484239823","playerLoc":"Powai","playerNick":"Random Nick"}</code>
シリアル化:
<code class="language-csharp">string jsonString = "{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}"; Player player = JsonUtility.FromJson<Player>(jsonString);</code>
出力: 大文字:
その他の予防策 デジタル属性またはデジタル属性を備えたJSONの場合:Unity WikiでSimpleJsonの使用を検討できます。
<code class="language-csharp">public static class JsonHelper { public static T[] FromJson<T>(string json) { Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json); return wrapper.Items; } public static string ToJson<T>(T[] array) { Wrapper<T> wrapper = new Wrapper<T>(); wrapper.Items = array; return JsonUtility.ToJson(wrapper); } private class Wrapper<T> { public T[] Items; } }</code>
クラスが配列ではなく、属性があり、メンバーが属性として定義されていないことを確認してください(delete)。
<code class="language-csharp">Player[] players = new Player[2]; players[0] = new Player { playerId = "8484239823", playerLoc = "Powai", playerNick = "Random Nick" }; players[1] = new Player { playerId = "512343283", playerLoc = "User2", playerNick = "Rand Nick 2" }; string playersToJson = JsonHelper.ToJson(players);</code>
<code class="language-json">{"Items":[{"playerId":"8484239823","playerLoc":"Powai","playerNick":"Random Nick"},{"playerId":"512343283","playerLoc":"User2","playerNick":"Rand Nick 2"}]}</code>
以上がJSONUTILITYを使用してJSONとJSONアレイを統一してシリアル化して脱着する方法は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。