protected void GetBody(object obj) { using (StreamReader reader = new StreamReader(this.Request.Body, Encoding.UTF8)) { var request = reader.ReadToEndAsync(); if (JsonTools.IsValidJson(request.Result)) { this.ModelDictionary = this.GetJson(request.Result, obj); } else { var json = JsonTools.QueryStringToJson(request.Result); this.ModelDictionary = this.GetJson(json, obj); } } }
private Dictionary <string, object> GetJson(string result, object obj) { var returnDictionary = new Dictionary <string, object>(); var jsonObject = (JObject)JsonConvert.DeserializeObject(result); if (jsonObject != null) { foreach (var token in jsonObject) { string propertyName = token.Key; var property = this.GetModelProperty(ref propertyName, obj); if (property == null || this.ModelDictionary.ContainsKey(propertyName)) { continue; } var type = property.PropertyType; if (property.GetCustomAttribute <InputAttribute>(false) != null) { var jsonString = token.Value.ToString(); var isValidJson = JsonTools.IsValidJson(jsonString); if (isValidJson && JToken.Parse(jsonString) is JArray) { var itemType = type.IsArray ? type.GetGenericArguments()[0] : type; if (itemType.GetConstructor(Type.EmptyTypes) != null && !itemType.IsAbstract) { var objectClass = Activator.CreateInstance(itemType); var list = new List <Dictionary <string, object> >(); foreach (var item in token.Value.ToObject <List <object> >()) { list.Add(this.GetJson(item.ToString(), objectClass)); } if (type.IsArray) { returnDictionary.Add(propertyName, list); } else { returnDictionary.Add(propertyName, list.FirstOrDefault()); } } else { returnDictionary.Add(propertyName, token.Value.ToObject <List <dynamic> >()); } } else { if (type.GetConstructor(Type.EmptyTypes) != null && !type.IsAbstract) { returnDictionary.Add(propertyName, token.Value.ToObject <Dictionary <string, object> >()); } else { var tokenObject = token.Value.ToObject <object>(); returnDictionary.Add(propertyName, tokenObject); } } } } } return(returnDictionary); }