litJson做爲優秀的第三方庫,是解析Json很好的工具。javascript
使用的第三方庫java
添加引用 litJson,以下兩個引用可直接添加System.ServiceModel.Web,System.Runtime.Serialization
using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using LitJson;
解析Json本地文件,能夠是.txt,能夠是如.json,.cgf其餘文件格式文件git
首先本地Json文件中的數據以下,是接口中解析出來的數據格式。github
{ "library": [{ "materialManufacturer": "11", "regularLabelling": "", "sheetLabelling": "" }, { "materialManufacturer": "fqwfewq", "regularLabelling": "", "sheetLabelling": "" }] }
//一個材料信息,包含以下成員 public class MaterialItem { public static List<MaterialItem> materialList = new List<MaterialItem>(); public string materialManufacturer { get; set; } public string regularLabelling { get; set; } public string sheetLabelling { get; set; } } //材料類 public class Material { public Material() { MaterialItem = new MaterialItem(); } public string matrail { get; set; } public MaterialItem MaterialItem { get; set; } }
/// <summary> /// 主方法 /// </summary> /// <param name="args"></param> public static void Main(string[] args) { //Json文件的具體路徑 string filePath = @"C:\Users\170125\Desktop\json\json.txt"; GetJsonData(filePath); }
/// <summary> /// Json解析的方法封裝 /// </summary> /// <param name="tmpUrlI">傳入的接口Url</param> /// <param name="tmpKeyName">簽名</param> /// <param name="tmpKeyValue">認證</param> public static void GetJsonData(string tmpFileI) { JsonData jsonData = JsonMapper.ToObject(File.ReadAllText(tmpFileI)); //若是獲取的Json文件不爲空 if (jsonData != null) { //取library下鍵值字典 JsonData jsonDicList = jsonData["library"]; //foreach循環 foreach (JsonData item in jsonDicList) { MaterialItem JO = new MaterialItem(); JO.materialManufacturer = item["materialManufacturer"].ToString(); JO.regularLabelling = item["regularLabelling"].ToString(); JO.sheetLabelling = item["sheetLabelling"].ToString(); //將信息添加到list集合中 MaterialItem.materialList.Add(JO); } } }
星星之火能夠燎原,今日點滴的付出,是往後的苦盡甘來。莫愁前路漫漫,天下誰人不識君。感謝你閱讀此文稿,也但願你能不吝賜教。推薦比較全面的我的學習網站,但願對你有幫助。json
var normalChild = { nickName : "墨客碼", site : "http://www.cnblogs.com/gss0525/" descTarget : ".net後臺開發者,熱衷分享技術,心懷感恩,深耕不綴。" }