Ejemplo n.º 1
0
 /// <summary>
 /// 输出字符串类型文本
 /// </summary>
 /// <returns></returns>
 public override string ToString()
 {
     if (Type == JsonType.Object)
     {
         if (isFormat)
         {
             return(JSONTools.JsonFormat(GetObjectString()));
         }
         return(GetObjectString());
     }
     if (Type == JsonType.Array)
     {
         if (isFormat)
         {
             return(JSONTools.JsonFormat(GetObjectString()));
         }
         return(GetObjectString());
     }
     if (Type == JsonType.String)
     {
         return(String.ToString());
     }
     if (Type == JsonType.Number)
     {
         return(Value.ToString());
     }
     if (Type == JsonType.Bool)
     {
         return(Value.ToString().ToLower());
     }
     if (Type == JsonType.Null)
     {
         return(Value.ToString().ToLower());
     }
     return(base.ToString());
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 嵌套递归获取JSON,输出字符串类型文本
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (Type == JsonType.Object)
            {
                TreeLevel++;
                string str = string.Empty;
                for (int i = 0; i < Dict.Count; i++)
                {
                    KeyValuePair <string, JsonValue> dictObj = Dict.ElementAt(i);
                    if (IsFormat)
                    {
                        str += JSONTools.TreeLevel(TreeLevel);
                    }
                    if (dictObj.Value == null)
                    {
                        str += "\"" + dictObj.Key + "\":";
                        if (IsFormat)
                        {
                            str += " ";
                        }
                        str += "null";
                    }
                    else
                    {
                        str += "\"" + dictObj.Key + "\":";
                        if (IsFormat)
                        {
                            str += " ";
                        }
                        str += dictObj.Value.SetTreeLevel(TreeLevel);
                    }
                    if (i < (Dict.Count - 1))
                    {
                        str += ",";
                        if (IsFormat)
                        {
                            str += "\n";
                        }
                    }
                }
                string endString = "{";
                if (IsFormat)
                {
                    endString += "\n";
                    endString += str;
                    endString += "\n" + JSONTools.TreeLevel(TreeLevel - 1);
                }
                else
                {
                    endString += str;
                }

                return(endString += "}");
            }
            if (Type == JsonType.Array)
            {
                TreeLevel++;
                string str = "";
                for (int i = 0; i < Arrays.Count; i++)
                {
                    if (IsFormat)
                    {
                        str += JSONTools.TreeLevel(TreeLevel);
                    }
                    JsonValue jsonValue = Arrays[i];
                    if (jsonValue == null)
                    {
                        str += "null";
                    }
                    else
                    {
                        str += jsonValue.SetTreeLevel(TreeLevel);
                    }
                    if (i < (Arrays.Count - 1))
                    {
                        str += ",";
                        if (IsFormat)
                        {
                            str += "\n";
                        }
                    }
                }
                string endString = "[";
                if (IsFormat)
                {
                    endString += "\n";
                    endString += str;
                    endString += "\n" + JSONTools.TreeLevel(TreeLevel - 1);
                }
                else
                {
                    endString += str;
                }

                return(endString += "]");
            }
            if (Type == JsonType.String)
            {
                return("\"" + String.ToString() + "\"");
            }
            if (Type == JsonType.Number)
            {
                return(Value.ToString());
            }
            if (Type == JsonType.Bool)
            {
                return(Value.ToString().ToLower());
            }
            if (Type == JsonType.Null)
            {
                return("null");
            }
            return(null);
        }