using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RetailCrm.Extra { class Tools { public static string httpBuildQuery(Dictionary data) { if (data is Dictionary == false) { return String.Empty; } var parts = new List(); HandleItem(data, parts); return String.Join("&", parts); } private static void HandleItem(object data, List parts, string prefix = "") { if (data == null) return; if (data is Dictionary) { parts.Add(FormatDictionary((Dictionary)data, prefix)); } else { parts.Add(String.IsNullOrEmpty(data.ToString()) ? String.Empty : String.Format("{0}={1}", prefix, data.ToString())); } } private static string FormatDictionary(Dictionary obj, string prefix = "") { var parts = new List(); foreach (KeyValuePair kvp in obj) { string newPrefix = string.IsNullOrEmpty(prefix) ? String.Format("{0}{1}", prefix, kvp.Key) : String.Format("{0}[{1}]", prefix, kvp.Key); HandleItem(kvp.Value, parts, newPrefix); } return String.Join("&", parts); } public static Dictionary jsonDecode(string json) { return jsonObjectToDictionary((Dictionary)JsonConvert.DeserializeObject>(json)); } private static Dictionary jsonObjectToDictionary(Dictionary data) { Dictionary result = new Dictionary(); foreach (KeyValuePair kvp in data) { System.Console.WriteLine(kvp.Key.ToString()); System.Console.WriteLine(kvp.Value.ToString()); System.Console.ReadLine(); object valueObj = kvp.Value; string value = String.Empty; if (valueObj.GetType() == typeof(JArray)) { string tmpValue = JsonConvert.SerializeObject(((JArray)valueObj).ToArray()); value = tmpValue.Replace("[", "{"); value = value.Replace("]", "}"); } else { value = valueObj.ToString(); } System.Console.WriteLine(value); System.Console.ReadLine(); if (value != "") { if (valueObj.GetType() == typeof(JObject) || valueObj.GetType() == typeof(JArray)) { valueObj = jsonObjectToDictionary((Dictionary)JsonConvert.DeserializeObject>(value)); } result.Add(kvp.Key.ToString(), valueObj); } } return result; } } }