0
我想要做的是將已保存在litjson文件中的數據添加到列表中,格式正確。所以我可以稍後在遊戲中將它稱爲圖標。這裏是代碼,所以應該很容易重新創建來測試我遇到的問題。如何將litJson保存文件中的數據添加到列表
JsonIcons
類:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class JsonIcons {
public string IconName;//Shows the icon Name in the list
public int IconID;// Shows the Icon ID in the list
public Sprite AssignIcon;
public JsonIcons(string Name, int ID)
{
IconName = Name;
IconID = ID;
}
public JsonIcons()
{
}
}
JsonTest
類:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
//This class that does the saving
public class JsonTest : MonoBehaviour {
public List<JsonIcons> JIcon = new List<JsonIcons>();
public JsonData JCD;
protected JsonIcons KZ, TestTK;
public void Start()
{
TestTK = new JsonIcons("Kagami", 40);
KZ = new JsonIcons("Magic", 0);
JIcon.Add(TestTK); //Add things to the list to be save
JIcon.Add(KZ);
JCD = JsonMapper.ToJson(JIcon);
//This is where I saved the things inside the JIcon list to a Json file
File.WriteAllText(Application.dataPath + "/JsonSaveTest.json", JCD.ToString());
//Debug.Log(JCD);
}
}
JsonReadTest
類:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;
public class JsonReadTest : MonoBehaviour {
public List<JsonIcons> ReadSJ = new List<JsonIcons>();
private string JString;
public JsonData IconData;
// Use this for initialization
void Start()
{
//Trying to get this file to load in the correct format inside the ReadSJ list
JString = File.ReadAllText(Application.dataPath + "/JsonFiles/JsonSaveTest.json");
IconData = JsonMapper.ToObject(JString);
}
}
我editted您的標題。請參閱[「應該在其標題中包含」標籤「的問題?」](http://meta.stackoverflow.com/questions/19190/),其中的共識是「不,他們不應該」。「 – Luizgrs
好的,這就是很抱歉,不知道。 –