1、引用最新的Newtonsoft.Json类如何
2、定义实体
public class TestModel
{
public string id { get; set; }
public string name{ get; set; }
public List<TestModel> list{ get; set; }
}
3、json文件内容如下,数组对象
[
{
"id": "2ab3a5ea-674d-4d3e-9b01-5f9934733eb1",
"name": "标题一"
"menuChildren": [
{
"id": "2ab3a5ea-674d-4d3e-9b01-5f9934733eb2",
"name": "标题1",
"menuChildren": []
},
{
"id": "2ab3a5ea-674d-4d3e-9b01-5f9934733eb2",
"name": "小标题1",
"menuChildren": []
}
]
}
]
3、读取json文件,获取json字符串,通过文件流获取
//path=文件在服务器的物理路径,绝对路径
using (StreamReader r = new StreamReader(path))
{
json = r.ReadToEnd();
}
4、读取json文件,获取json字符串,并转为实体类
List<TestModel> jsonModel = FileHelper.GetJsonModel<List<TestModel>>("/test.json");