嘗試從僅使用Newtonsoft.Json的C#將所有信息從json文件轉換爲數組。JSON到數組C#
namespace tslife
{
partial class game
{
world[] game_intro = _read_world<world>("intro");
//** other code **//
public void update()
{
//crashes: System.NullReferenceException: Object reference not set to an instance of an object
Console.WriteLine(game_intro[0].data.Text);
}
private static T[] _read_world<T>(string level)
{
var json_data = string.Empty;
string st = "";
try
{
var stream = File.OpenText("Application/story/"+level+".json");
//Read the file
st = stream.ReadToEnd();
}
catch(SystemException e){}
json_data = st;
//Console.WriteLine(json_data);
// if string with JSON data is not empty, deserialize it to class and return its instance
T[] dataObject = JsonConvert.DeserializeObject<T[]>(json_data);
return dataObject;
}
}
}
public class worldData {
public string Text { get; set; }
public string Icon { get; set; }
public int sectionID { get; set; }
}
public class world
{
public worldData data;
}
我不知道它是否是json的格式,但是我在搜索其他地方後卡住了。
[{
"world":
{
"Text":"Hi",
"Icon":"image01.png",
"sectionID": 0
}
},
{
"world":
{
"Text":"Hey",
"Icon":"image02.png",
"sectionID": 1
}
}
]
你可以嘗試更換'公共worldData數據;'與公共worldData世界{get;設置;}'讓我們知道會發生什麼? – rene 2014-11-01 13:14:50
我原來是這樣,仍然沒有工作。 – 2014-11-01 13:17:56
你得到一個空的數組,對吧?你能擺脫那空空的漁獲嗎? – rene 2014-11-01 13:19:33