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);
                  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章