C#操作json的LitJson.dll
https://download.csdn.net/download/pukuimin1226/5851711
讀取json
//UserInfo.json
[ {"id":"1","name":"葉良辰","age": "20"}, {"id":"2","name":"趙日天","age": "25"} ]
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("UserInfo.json")); foreach (JsonData temp in jsonData) {
string id=temp["id"].ToString(); string name = temp["name"].ToString(); string age = temp["age"].ToString(); Console.WriteLine("id:"+id+" name:"+name+" age:"+age); } Console.ReadLine();
class UserInfo { public string id { get; set; } public string name { get; set; } public string age { get; set; } }
上面的方法其實是比較麻煩的下面介紹 可以直接利用泛型直接返回集合
//Json的鍵必須和類的屬性或欄位匹配區分大小寫
UserInfo[] jsonData = JsonMapper.ToObject<UserInfo[]>(File.ReadAllText("UserInfo.json")); foreach (UserInfo temp in jsonData) { Console.WriteLine(temp.id+temp.name+temp.age); } Console.ReadLine();