如何将 fastJSON 与 JSON 数组一起使用
How use fastJSON with a JSON array
我正在使用 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;
}
}
我不知道如何迭代名称为"enemies"的数组数据。
按照您当前编写代码的方式,您将像这样遍历敌人:
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;
}
}
我正在使用 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;
}
}
我不知道如何迭代名称为"enemies"的数组数据。
按照您当前编写代码的方式,您将像这样遍历敌人:
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;
}
}