/// <summary> /// 解析Json转换成Entity /// </summary> /// <typeparam name="T"></typeparam> /// <param name="response"></param> /// <param name="entityName"></param> /// <remarks> /// http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing /// http://james.newtonking.com/json/help/index.html?topic=html/QueryJsonDynamic.htm /// http://james.newtonking.com/json/help/index.html?topic=html/LINQtoJSON.htm /// </remarks> /// <returns></returns> public List <T> TryParseEntitys <T>(WebApiConsumerResponse response, string entityName) { if (response == null || string.IsNullOrWhiteSpace(response.Content)) { return(null); } //dynamic dynamicJson = JObject.Parse(response.Content); //foreach (dynamic customer in dynamicJson.value) //{ // string str = string.Format("{0} {1} {2}", customer.Id, customer.CustomerGuid, customer.Email); // Debug.WriteLine(str); //} var json = JObject.Parse(response.Content); string metadata = (string)json["odata.metadata"]; if (!string.IsNullOrWhiteSpace(metadata) && metadata.Contains(entityName)) { var entitys = json["value"].Select(x => x.ToObject <T>()).ToList(); return(entitys); } return(null); }
public object TryParseEntity <T>(WebApiConsumerResponse response, string entityName, IContractResolver contractResolver) { if (response == null || string.IsNullOrWhiteSpace(response.Content)) { return(null); } //dynamic dynamicJson = JObject.Parse(response.Content); //foreach (dynamic customer in dynamicJson.value) //{ // string str = string.Format("{0} {1} {2}", customer.Id, customer.CustomerGuid, customer.Email); // Debug.WriteLine(str); //} var json = JObject.Parse(response.Content); string metadata = (string)json["odata.metadata"]; if (!string.IsNullOrWhiteSpace(metadata) && metadata.Contains(entityName)) { // var entitys = json.ToObject(typeof(T)); var entitys = JsonConvert.DeserializeObject <T>(response.Content, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = contractResolver }); return(entitys); } return(null); }
/// <summary> /// 解析Json 返回动态对象 /// </summary> /// <param name="response"></param> /// <returns></returns> public dynamic TryParseDynamics(WebApiConsumerResponse response) { if (response == null || string.IsNullOrWhiteSpace(response.Content)) { return(null); } dynamic dynamicJson = JObject.Parse(response.Content); return(dynamicJson.value); }
private void GetResponse(HttpWebResponse webResponse, WebApiConsumerResponse response) { if (webResponse == null) { return; } response.Status = string.Format("{0} {1}", (int)webResponse.StatusCode, webResponse.StatusDescription); response.Headers = webResponse.Headers.ToString(); using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { response.Content = reader.ReadToEnd(); } }
public bool ProcessResponse(HttpWebRequest webRequest, WebApiConsumerResponse response) { if (webRequest == null) { return(false); } bool result = true; HttpWebResponse webResponse = null; try { webResponse = webRequest.GetResponse() as HttpWebResponse; GetResponse(webResponse, response); } catch (WebException wexc) { result = false; webResponse = wexc.Response as HttpWebResponse; GetResponse(webResponse, response); } catch (Exception exc) { result = false; response.Content = string.Format("{0}\r\n{1}", exc.Message, exc.StackTrace); } finally { if (webResponse != null) { webResponse.Close(); webResponse.Dispose(); } } return(result); }