C#操作json

C#操作json的LitJson.dll

https://download.csdn.net/download/pukuimin1226/5851711

读取json

//UserInfo.json
<br />[
	{"id":"1","name":"叶良辰","age": "20"},
	{"id":"2","name":"赵日天","age": "25"}
]
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("UserInfo.json"));
            foreach (JsonData temp in jsonData)
            {<br />          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的键必须和类的属性或字段匹配区分大小写<br />         UserInfo[] jsonData = JsonMapper.ToObject<UserInfo[]>(File.ReadAllText("UserInfo.json"));

            foreach (UserInfo temp in jsonData)
            {
                Console.WriteLine(temp.id+temp.name+temp.age);
            }
            Console.ReadLine();