首页 > 技术文章 > C#使用litJson解析Json(二)

gss0525 2019-01-02 18:07 原文

目录

  • 使用的第三方库
  • 添加的引用
  • 举例说明
  • 小结

阐述

  • litJson作为优秀的第三方库,是解析Json很好的工具。

  • 使用的第三方库

 添加引用 litJson,如下两个引用可直接添加System.ServiceModel.Web,System.Runtime.Serialization
  • 添加using指令集
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using LitJson;

举例子说明

  • 解析Json本地文件,可以是.txt,可以是如.json,.cgf其他文件格式文件

  • 首先本地Json文件中的数据如下,是接口中解析出来的数据格式。

{
	"library": [{
		"materialManufacturer": "11",
		"regularLabelling": "",
		"sheetLabelling": ""
	}, {
		"materialManufacturer": "fqwfewq",
		"regularLabelling": "",
		"sheetLabelling": ""
	}]
}
  • 先定义两个类,第一个类内容和之前的一致,此处要根据Json数据灵活变通,刚开始理解Json文件中的数据结构很重要。不过Json长得都差不多。
       //一个材料信息,包含如下成员
        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);
        }
  • 解析Json的方法封装,没有GET请求这就更简单了,从指定路径中读取文件信息转化成JsonData类,然后再序列化里面的数据。
        /// <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是数据与数据之间进行交互,很快捷的方式之一。
  • 程序一般都遵循 Don't repeat yourself,因为程序代码很多,核心的东西却是唯一的。希望你能找到这两篇文章的交集,然后衍生出自己的思路和想法。

感激

星星之火可以燎原,今日点滴的付出,是日后的苦尽甘来。莫愁前路漫漫,天下谁人不识君。感谢你阅读此文稿,也希望你能不吝赐教。推荐比较全面的个人学习网站,希望对你有帮助。

关于作者

  var normalChild = {
    nickName  : "墨客码",
    site : "http://www.cnblogs.com/gss0525/"
    descTarget : ".net后台开发者,热衷分享技术,心怀感恩,深耕不缀。"
  }

推荐阅读