net core 使用Newtonsoft.Json 读取Json文件数据

使用Newtonsoft.Json库实现解析上传的Json格式文件的数据

以以下的Json文件格式为例

{
  "Dictionary": [
    {
      "DictionaryKey": "学历",
      "DictionaryValue": "学历",
      "ParentKey": "",
      "IsKey": 1
    },
    {
      "DictionaryKey": "本科",
      "DictionaryValue": "本科",
      "ParentKey": "学历",
      "IsKey": 0
    },
    {
      "DictionaryKey": "大专",
      "DictionaryValue": "大专",
      "ParentKey": "学历",
      "IsKey": 0
    },
    {
      "DictionaryKey": "硕士",
      "DictionaryValue": "硕士",
      "ParentKey": "学历",
      "IsKey": 0
    },
    {
      "DictionaryKey": "博士",
      "DictionaryValue": "博士",
      "ParentKey": "学历",
      "IsKey": 0
    },
    {
      "DictionaryKey": "设备类型",
      "DictionaryValue": "设备类型",
      "ParentKey": "",
      "IsKey": 1
    },
    {
      "DictionaryKey": "大型",
      "DictionaryValue": "大型",
      "ParentKey": "设备类型",
      "IsKey": 0
    },
    {
      "DictionaryKey": "工作地点",
      "DictionaryValue": "工作地点",
      "ParentKey": "",
      "IsKey": 1
    },
    {
      "DictionaryKey": "常州软件园",
      "DictionaryValue": "常州软件园",
      "ParentKey": "工作地点",
      "IsKey": 0
    },
    {
      "DictionaryKey": "设备大类",
      "DictionaryValue": "设备大类",
      "ParentKey": "",
      "IsKey": 1
    },
    {
      "DictionaryKey": "大新机械",
      "DictionaryValue": "大新机械",
      "ParentKey": "设备大类",
      "IsKey": 0
    }
  ],
  "ApplicationInfo": {
    "ApplicationFlag": "EQSystem",
    "ApplicationName": "设备管理",
    "ApplicationType": "Web",
    "DataBaseConnectString": "",
    "IsEnable": 1,
    "ApplicationDescribe": "rrr"
  }
}

实体类

public class DictionaryImport
    {
        /// <summary>
        /// 字典Key
        /// </summary>
        public string DictionaryKey { get; set; }
        /// <summary>
        /// 字典Value
        /// </summary>
        public string DictionaryValue { get; set; }
        /// <summary>
        /// 所属字典Key
        /// </summary>
        public string ParentKey { get; set; }
        /// <summary>
        /// 是否Key 0:否 1:是
        /// </summary>
        public int IsKey { get; set; }
    }
}
public class ApplicationInfo
    {
        /// <summary>
        /// 应用标识
        /// </summary>
        public string ApplicationFlag { get; set; }
        /// <summary>
        /// 应用名称
        /// </summary>
        public string ApplicationName { get; set; }        
        /// <summary>
        /// 应用类别 App、Web
        /// </summary>
        public string ApplicationType { get; set; }
        /// <summary>
        /// 应用数据库连接
        /// </summary>
        public string DataBaseConnectString { get; set; }
        /// <summary>
        /// 是否可用 0:否 1:是
        /// </summary>
        public int IsEnable { get; set; }
        /// <summary>
        /// 应用描述
        /// </summary>
        public string ApplicationDescribe { get; set; }

    }

json文件解析代码实现

                IFormFileCollection formFiles = Request.Form.Files;//上传的文件
                foreach (IFormFile file in formFiles)
                {
                    var stream = file.OpenReadStream();
                    System.IO.StreamReader streamReader = new System.IO.StreamReader(stream);
                    JsonTextReader jsonTextReader = new JsonTextReader(streamReader);
                    JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
                    //JToken token = jsonObject["Dictionary"];
                    //foreach (JObject e in token)
                    //{
                    //    DictionaryImport dictionaryImport = e.ToObject<DictionaryImport>();
                    //}
                    string strDictionaryJson = jsonObject["Dictionary"].ToString();//json字符串
                    string strApplicationJson= jsonObject["ApplicationInfo"].ToString();
                    //所属应用数据解析
                    var applicationInfoImport = JsonConvert.DeserializeObject<ApplicationInfo>(strApplicationJson);
                    //字典值数据解析
                    List<DictionaryImport> dictionaryImports = JsonConvert.DeserializeObject<List<DictionaryImport>>(strDictionaryJson);
                  }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值