Ejemplo n.º 1
0
        void ToJson()
        {
            var txt = RequestDocument["txt"].ToString();

            txt = RemoveEscapeChars(txt);

            var strList = new List <string>();

            try
            {
                var lines = txt.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                if (lines.All(t => t.Contains(":")))
                {
                    foreach (var item in lines)
                    {
                        var keyValues = item.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (keyValues.Length == 2)
                        {
                            var key   = keyValues[0].Trim();
                            var value = keyValues[1].Trim();

                            value = System.Web.HttpUtility.UrlDecode(value);
                            value = value.Replace("\"", "\\\"");

                            if (!string.IsNullOrEmpty(key))
                            {
                                if (value == "true" || value == "false" || IsNum(value))
                                {
                                    strList.Add($"\"{key}\":{value},");
                                }
                                else
                                {
                                    strList.Add($"\"{key}\":\"{value}\",");
                                }
                            }
                        }
                    }
                }
                else
                {
                    var items = txt.Split(new[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
                    if (items.All(t => t.Contains("=")))
                    {
                        foreach (var item in items)
                        {
                            var keyValues = item.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                            if (keyValues.Length == 2)
                            {
                                var key   = keyValues[0].Trim();
                                var value = keyValues[1].Trim();

                                value = System.Web.HttpUtility.UrlDecode(value);
                                value = value.Replace("\"", "\\\"");

                                if (!string.IsNullOrEmpty(key))
                                {
                                    if (value == "true" || value == "false" || IsNum(value))
                                    {
                                        strList.Add($"\"{key}\":{value},");
                                    }
                                    else
                                    {
                                        strList.Add($"\"{key}\":\"{value}\",");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            //rpc调用(包括网站)
            var jd = new JsonData()
            {
                ["txt"] = string.Empty
            };

            if (strList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("{");

                strList[strList.Count - 1] = strList.Last().TrimEnd(',');
                foreach (var str in strList)
                {
                    stringBuilder.Append(str);
                }

                stringBuilder.Append("}");

                jd["txt"] = RichTextBoxHelper.FormatJsonString(stringBuilder.ToString());
            }
            else if (JsonSerializerHelper.IsJsonString(txt))
            {
                jd["txt"] = RichTextBoxHelper.FormatJsonString(txt);
            }
            else
            {
                jd["txt"] = string.Empty;
            }

            Json((int)ReturnStatus_Ext.Success, string.Empty, jd);
        }
Ejemplo n.º 2
0
        private void btnToJson_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox.Text))
            {
                return;
            }

            var txt = RemoveEscapeChars(textBox.Text);

            var strList = new List <string>();

            try
            {
                var lines = txt.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                if (lines.All(t => t.Contains(":")))
                {
                    foreach (var item in lines)
                    {
                        var keyValues = item.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (keyValues.Length == 2)
                        {
                            var key   = keyValues[0].Trim();
                            var value = keyValues[1].Trim();

                            value = System.Web.HttpUtility.UrlDecode(value);
                            value = value.Replace("\"", "\\\"");

                            if (!string.IsNullOrEmpty(key))
                            {
                                if (value == "true" || value == "false" || IsNum(value))
                                {
                                    strList.Add($"\"{key}\":{value},");
                                }
                                else
                                {
                                    strList.Add($"\"{key}\":\"{value}\",");
                                }
                            }
                        }
                    }
                }
                else
                {
                    var items = txt.Split(new[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
                    if (lines.All(t => t.Contains("=")))
                    {
                        foreach (var item in items)
                        {
                            var keyValues = item.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                            if (keyValues.Length == 2)
                            {
                                var key   = keyValues[0].Trim();
                                var value = keyValues[1].Trim();

                                value = System.Web.HttpUtility.UrlDecode(value);
                                value = value.Replace("\"", "\\\"");

                                if (!string.IsNullOrEmpty(key))
                                {
                                    if (value == "true" || value == "false" || IsNum(value))
                                    {
                                        strList.Add($"\"{key}\":{value},");
                                    }
                                    else
                                    {
                                        strList.Add($"\"{key}\":\"{value}\",");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (strList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("{");

                strList[strList.Count - 1] = strList.Last().TrimEnd(',');
                foreach (var str in strList)
                {
                    stringBuilder.Append(str);
                }

                stringBuilder.Append("}");

                textBox.Text = RichTextBoxHelper.FormatJsonString(stringBuilder.ToString());

                btnCompressEscapeChars.Visible = true;
            }
            else if (JsonSerializerHelper.IsJsonString(txt))
            {
                textBox.Text = RichTextBoxHelper.FormatJsonString(txt);
                btnCompressEscapeChars.Visible = true;
            }
            else
            {
                textBox.Text = string.Empty;
                btnCompressEscapeChars.Visible = false;
            }
        }