Ejemplo n.º 1
0
 /// <summary>
 /// 将Json 数组分隔成多个键值对。
 /// </summary>
 public static List <Dictionary <string, string> > SplitArray(string jsonArray)
 {
     if (string.IsNullOrEmpty(jsonArray))
     {
         return(null);
     }
     jsonArray = jsonArray.Trim();
     return(JsonSplit.Split(jsonArray));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 将Json分隔成键值对。
        /// </summary>
        public static Dictionary <string, string> Split(string json)
        {
            var result = JsonSplit.Split(json);

            if (result != null && result.Count > 0)
            {
                return(result[0]);
            }
            return(null);
        }
Ejemplo n.º 3
0
        private static string GetXml(Dictionary <string, string> dic, bool isWithAttr)
        {
            var sb     = Pool.StringBuilder.Get();
            var isJson = false;

            foreach (var item in dic)
            {
                isJson = IsJson(item.Value);
                if (!isJson)
                {
                    sb.AppendFormat("<{0}>{1}</{0}>", item.Key, FormatCdata(item.Value));
                }
                else
                {
                    var jsonList = JsonSplit.Split(item.Value);
                    if (jsonList != null && jsonList.Count > 0)
                    {
                        if (!isWithAttr)
                        {
                            sb.AppendFormat("<{0}>", item.Key);
                        }
                        for (var j = 0; j < jsonList.Count; j++)
                        {
                            if (isWithAttr)
                            {
                                sb.Append(GetXmlElement(item.Key, jsonList[j]));
                            }
                            else
                            {
                                sb.Append(GetXml(jsonList[j], isWithAttr));
                            }
                        }
                        if (!isWithAttr)
                        {
                            sb.AppendFormat("</{0}>", item.Key);
                        }
                    }
                    else // 空Json {}
                    {
                        sb.AppendFormat("<{0}></{0}>", item.Key);
                    }
                }
            }
            return(sb.Put());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 将一个Json转成Xml
        /// </summary>
        /// <param name="json">Json字符串</param>
        /// <param name="isWithAttr">是否转成属性,默认true</param>
        /// <returns></returns>
        public static string ToXml(string json, bool isWithAttr)
        {
            var sb = Pool.StringBuilder.Get();

            sb.Append("<?xml version=\"1.0\"  standalone=\"yes\"?>");
            var dicList = JsonSplit.Split(json);

            if (dicList != null && dicList.Count > 0)
            {
                var addRoot = dicList.Count > 1 || dicList[0].Count > 1;
                if (addRoot)
                {
                    sb.Append("<root>");//</root>";
                }

                sb.Append(GetXmlList(dicList, isWithAttr));

                if (addRoot)
                {
                    sb.Append("</root>");//</root>";
                }
            }
            return(sb.Put());
        }