C#使用litJson解析Json(一)

目錄

  • 使用的第三方庫
  • 添加的引用
  • 適用場景
  • 舉例說明
  • 小結

闡述

  • litJson做爲優秀的第三方庫,是解析Json很好的工具。javascript

  • 使用的第三方庫java

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

適用情景

  • 解析Json字符串
  • 解析本地Json文件
  • 解析Json接口Api,解析帶簽名的Json接口

舉例子說明

  • 解析Json接口,帶簽名的Json接口git

  • 首先Json字符串內容以下,是接口url的內容。大體能夠看到,字典裏面嵌套着字典,咱們能夠先取library中的字典,再取字典中的Key和Value值。
  • 解析固定格式的Json,還有讀取本地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)
        {

            //接口url
            string url = " http://192.168.42.46/Windchill/servlet/Navigation/MaterialLibraryServlet ";
            //簽名
            string restName = "Authorization";
            //認證
            string restValue = "Basic d2NhZG1pbjp3Y2FkbWlu";
            //傳入參數,讀取Json字符串
            GetJsonData(url, restName, restValue);
        }
  • 解析Json的方法封裝,邏輯也是比較清晰,GET請求,請求頭的添加,獲取數據流文件,再父級Key下的字典,再從字典中取鍵值對。最後添加到list集合中,搞定數據解析。如何利用lis結合要結合具體的業務場景。
/// <summary>
        /// Json解析的方法封裝
        /// </summary>
        /// <param name="tmpUrlI">傳入的接口Url</param>
        /// <param name="tmpKeyName">簽名</param>
        /// <param name="tmpKeyValue">認證</param>
        public static void GetJsonData(string tmpUrlI, string tmpKeyName, string tmpKeyValue)
        {
            //獲取請求
            HttpWebRequest request = WebRequest.Create(tmpUrlI) as HttpWebRequest;
            //加入請求頭
            request.Headers.Add(tmpKeyName, tmpKeyValue);
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                //獲取響應數據流
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                //得到json字符串
                string jsonstr = reader.ReadLine();
                JsonData jsonData = JsonMapper.ToObject(jsonstr);
                if (jsonData != null)
                {
                    //取library下鍵值字典
                    JsonData jsonDicList = jsonData["library"];
                    foreach (JsonData item in jsonDicList)
                    {
                        MaterialItem JO = new MaterialItem();
                        JO.materialManufacturer = item["materialManufacturer"].ToString();
                        JO.regularLabelling = item["regularLabelling"].ToString();
                        JO.sheetLabelling = item["sheetLabelling"].ToString();
                        MaterialItem.materialList.Add(JO);
                    }
                }
            }
        }

小結

  • Json是數據與數據之間進行交互,很快捷的方式之一。
  • 隨着互聯網產業的發展。手機應用,網站建設,Json更是成爲開發者,必不可少的技能之一。
  • get,post請求,待參數的Json,之後都會詳情介紹。

感激

星星之火能夠燎原,今日點滴的付出,是往後的苦盡甘來。莫愁前路漫漫,天下誰人不識君。感謝你閱讀此文稿,也但願你能不吝賜教。推薦比較全面的我的學習網站,但願對你有幫助。json

關於做者

var normalChild = {
    nickName  : "墨客碼",
    site : "http://www.cnblogs.com/gss0525/"
    descTarget : ".net後臺開發者,熱衷分享技術,心懷感恩,深耕不綴。"
  }
相關文章
相關標籤/搜索