.net 读取json 文件

原文:http://blog.csdn.net/w200221626/article/details/52169250

json 文件 格式:

 1 {
 2     "total": 1,
 3     "page": 1,
 4     "records": 1,
 5     "costtime": "100",
 6     "rows": [
 7         {
 8             "rownum": 1,
 9             "userid": "888",
10             "code": "8888",
11             "account": "8888",
12             "realname": "飞哥",
13             "spell": "ZM",
14             "gender": "",
15             "mobile": "15201956999",
16             "telephone": "021-88888888",
17             "email": "qq@qq.com",
18             "companyid": "10000",
19             "companyname": "上海",
20             "departmentid": "100001",
21             "departmentname": "技术部",
22             "duty": "工程师",
23             "enabled": 1,
24             "logoncount": null,
25             "lastvisit": null,
26             "sortcode": 9000,
27             "createuserid": "admin",
28             "remark": " "
29         }
30     ]
31 }
View Code

读取 json 文件:

 1         public string GetFileJson(string filepath)
 2         {
 3             string json = string.Empty;
 4             using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
 5             {
 6                 using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
 7                 {
 8                     json = sr.ReadToEnd().ToString();
 9                 }
10             }
11             return json;
12         }
View Code

调用方法 :

1 string filepath = Server.MapPath("~/json1.json");  
2 string json = GetFileJson(filepath);  
View Code

 

posted @ 2018-03-09 13:40  caolingyi  阅读(4587)  评论(0编辑  收藏  举报