1

私はfastJSONを使用して、作成した JSON ファイルからデータを読み取ります (JSON ファイルには、Unity のプロジェクトのゲームのレベルのデータが含まれていますが、それは重要ではありません)。これは JSON コンテンツです。

{"1": {
    "background": "background1.png",
    "description": "A description of this level",
    "enemies": 
              [{"name": "enemy1", "number": "5"},
               {"name": "enemy2", "number": "2"}]},
 "2": {
    "background": "background1.png",
    "description": "A description of this level",
    "enemies": 
              [{"name": "enemy1", "number": "8"},
               {"name": "enemy2", "number": "3"}]},
"3": {
    "background": "background2.png",
    "description": "A description of this level",
    "enemies": 
              [{"name": "enemy2", "number": "5"},
               {"name": "enemy3", "number": "3"},
               {"name": "enemy4", "number": "1"}]}
}

これは私のコードです:

using UnityEngine;
using System.Collections.Generic;
using fastJSON;

public class LevelManager : MonoBehaviour {

    private Dictionary<string, object> currentLevelData;

    public TextAsset levelJSON;
    public int currentLevel;

    // Use this for initialization
    void Start () {

        currentLevelData = LevelElements (currentLevel);
        if (currentLevelData != null) {
            Debug.Log (currentLevelData["background"]);
            Debug.Log (currentLevelData["description"]);
            /* How iterate the "enemies" array */
        } else {
            Debug.Log ("Could not find level '" + currentLevel + "' data");
        }
    }

    Dictionary<string, object> LevelElements (int level) {
        string jsonText = levelJSON.ToString();
        Dictionary<string, object> dictionary = fastJSON.JSON.Parse(jsonText) as Dictionary<string, object>;

        Dictionary<string, object> levelData = null;
        if (dictionary.ContainsKey (level.ToString ())) {
            levelData = dictionary [level.ToString ()] as Dictionary<string, object>;
        }

        return levelData;
    }
}

「敵」という名前の配列データをどのように反復するかわかりません。

4

1 に答える 1

1

コードが現在書かれている方法では、次のように敵を反復します。

foreach (Dictionary<string, object> enemy in (List<object>)currentLevelData["enemies"])
{
    Debug.Log(enemy["name"]);
    Debug.Log(enemy["number"]);
}

ただし、データを受け取るためにいくつかの強く型付けされたクラスを作成することをお勧めします。

public class Level
{
    public string background { get; set; }
    public string description { get; set; }
    public List<Enemy> enemies { get; set; }
}

public class Enemy
{
    public string name { get; set; }
    public string number { get; set; }
}

理想的には、これにより次のように逆シリアル化できます。

Dictionary<string, Level> dictionary = 
                          fastJSON.JSON.ToObject<Dictionary<string, Level>>(jsonText);

残念ながら、fastJSON ではこれを処理できないようです (試してみたところ、例外が発生しました)。ただし、 Json.Netのようなより堅牢なライブラリに切り替えると、問題なく動作します。

Dictionary<string, Level> dictionary = 
                        JsonConvert.DeserializeObject<Dictionary<string, Level>>(jsonText);

これにより、コードを書き直して、データをより簡単に操作できるようになります。

public class LevelManager : MonoBehaviour
{
    private Level currentLevelData;

    public string levelJSON;
    public int currentLevel;

    // Use this for initialization
    void Start()
    {
        currentLevelData = LevelElements(currentLevel);
        if (currentLevelData != null)
        {
            Debug.Log(currentLevelData.background);
            Debug.Log(currentLevelData.description);

            foreach (Enemy enemy in currentLevelData.enemies)
            {
                Debug.Log(enemy.name);
                Debug.Log(enemy.number);
            }
        }
        else
        {
            Debug.Log("Could not find level '" + currentLevel + "' data");
        }
    }

    Level LevelElements(int level)
    {
        string jsonText = levelJSON.ToString();
        Dictionary<string, Level> dictionary = 
                JsonConvert.DeserializeObject<Dictionary<string, Level>>(jsonText);

        Level levelData = null;
        if (dictionary.ContainsKey(level.ToString()))
        {
            levelData = dictionary[level.ToString()];
        }

        return levelData;
    }
}
于 2016-10-16T19:26:04.050 に答える