在做前台页面展示的时候需要后台数据,在前后分离的情况之下,我们以假数据来展示我们的逻辑。
脱离真实的数据库,来读取.json文件
第一、创建.json文件
{
"total": 1,
"page": 1,
"records": 1,
"costtime": "100",
"rows": [
{
"rownum": 1,
"userid": "888",
"code": "8888",
"account": "8888",
"realname": "飞哥",
"spell": "ZM",
"gender": "男",
"mobile": "15201956999",
"telephone": "021-88888888",
"email": "qq@qq.com",
"companyid": "10000",
"companyname": "上海",
"departmentid": "100001",
"departmentname": "技术部",
"duty": "工程师",
"enabled": 1,
"logoncount": null,
"lastvisit": null,
"sortcode": 9000,
"createuserid": "admin",
"remark": " "
}
]
}
第二、读取.json文件
public string GetFileJson(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
{
json = sr.ReadToEnd().ToString();
}
}
return json;
}第三、返回.json文件
public ActionResult GridPageListJson()
{
try
{
string filepath = Server.MapPath("~/Views/OrderInfoKJ/json1.json");
string json = GetFileJson(filepath);
return Content(json);
}
catch (Exception ex)
{
return null;
}
}第四、展示.json文件

本文介绍了一种在前后端分离开发模式下使用JSON文件模拟后台数据的方法。包括创建JSON文件、读取JSON文件并将其返回到前端进行展示的具体步骤。
1871

被折叠的 条评论
为什么被折叠?



