litJson做为优秀的第三方库,是解析Json很好的工具。javascript
使用的第三方库java
添加引用 litJson,以下两个引用可直接添加System.ServiceModel.Web,System.Runtime.Serialization
using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using LitJson;
解析Json接口,带签名的Json接口git
解析固定格式的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); }
/// <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
var normalChild = { nickName : "墨客码", site : "http://www.cnblogs.com/gss0525/" descTarget : ".net后台开发者,热衷分享技术,心怀感恩,深耕不缀。" }