API常用后台解析XML,JSON数据的两种方式(JSON)
/// <summary> /// 获取指定物品编号的数据(ebay) /// </summary> /// <param name="appid">ebayApi appid</param> /// <param name="_IncludeSelector">查询内容(Details,ShippingCosts,Variations,Description)</param> /// <param name="itemid">物品编号</param> /// <returns></returns> public string GetItem(string appID, string _IncludeSelector, string itemid) { string url = string.Format("http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=JSON&appid={0}&siteid=0&version=863&IncludeSelector={1}&ItemID={2}", appID.Trim(), _IncludeSelector, itemid.Trim()).Trim(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "Get"; req.ContentType = "application/json"; req.ReadWriteTimeout = 12000; //12秒链接不成功就中断 HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); var respneshtml = string.Empty; using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8)) { respneshtml = sr.ReadToEnd(); } return respneshtml; }
这是一个Ebay的获取物品详情的API
//GET DATE string getdate = _goodBLL.GetItem(appID, _IncludeSelector, itemid); //转换为JSON格式 JObject o = JObject.Parse(getdate); //属性赋值 //物品描述 if (o["Item"]["Description"] != null) { _itemdateModel.Item_Description = o["Item"]["Description"].ToString(); }
常规正常一个父节点里面包含一个子节点就用上面的方式就可以获取到
非常规一个父节点对应一个子节点的使用JA
//图片 List<string> picture = new List<string>(); //获取图片数量 if (o["Item"]["PictureURL"] != null) { int Picturecount = ((JArray)o["Item"]["PictureURL"]).Count; //给图片绑值 if (Picturecount > 0) { for (int picIndex = 0; picIndex < Picturecount; picIndex++) { picture.Add(o["Item"]["PictureURL"][picIndex].ToString()); } } //给图片绑值结束 //图片赋值 _itemdateModel.Item_PictureURL = picture; }
这就是JSON的后台解析方式也是很简单~ 不积跬步无以至千里,不计小流无以成江河
rray