using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace CSJson
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
string text = RichTextBox1.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
try
{
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(text);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
RichTextBox1.Text = textWriter.ToString();
}
}
catch
{
MessageBox.Show("无法格式化JSON文本。", "格式化失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Button3_Click(object sender, EventArgs e)
{
string text = RichTextBox1.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
try
{
StringBuilder sb = new StringBuilder();
using (StringReader reader = new StringReader(text))
{
int ch = -1;
int lastch = -1;
bool isQuoteStart = false;
while ((ch = reader.Read()) > -1)
{
if ((char)lastch != '\\' && (char)ch == '"')
{
if (!isQuoteStart)
{
isQuoteStart = true;
}
else
{
isQuoteStart = false;
}
}
if (!Char.IsWhiteSpace((char)ch) || isQuoteStart)
{
sb.Append((char)ch);
}
lastch = ch;
}
}
RichTextBox1.Text = sb.ToString();
}
catch
{
MessageBox.Show("无法压缩JSON文本。", "压缩失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Button4_Click(object sender, EventArgs e)
{
RichTextBox1.Text = string.Empty;
}
private void Button5_Click(object sender, EventArgs e)
{
string text = RichTextBox1.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
Clipboard.SetText(text);
MessageBox.Show("内容已复制。", "复制成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Button1_Click(object sender, EventArgs e)
{
string text = RichTextBox1.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
try
{
var root = JToken.Parse(text);
DisplayTreeView(root, "JSON");
}
catch
{
MessageBox.Show("无法解析JSON文本。", "解析失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void DisplayTreeView(JToken root, string rootName)
{
TreeView1.BeginUpdate();
try
{
TreeView1.Nodes.Clear();
var tNode = TreeView1.Nodes[TreeView1.Nodes.Add(new TreeNode(rootName))];
tNode.Tag = root;
AddNode(root, tNode);
//TreeView1.ExpandAll();
TreeView1.Nodes[0].Expand();
}
finally
{
TreeView1.EndUpdate();
}
}
private void AddNode(JToken token, TreeNode inTreeNode)
{
if (token == null)
return;
//加入值
//if (token is JValue)
//{
// var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(token.ToString()))];
// childNode.Tag = token;
//}
//else if (token is JObject)
if (token is JObject obj)
{
foreach (var property in obj.Properties())
{
var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(property.Name))];
childNode.Tag = property;
AddNode(property.Value, childNode);
}
}
else if (token is JArray array)
{
for (int i = 0; i < array.Count; i++)
{
var childNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode("[" + i.ToString() + "]"))];
childNode.Tag = array[i];
AddNode(array[i], childNode);
}
}
}
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
TextBox3.Text = e.Node.FullPath;
RichTextBox2.Text = e.Node.Tag.ToString();
} catch { }
}
private void Button6_Click(object sender, EventArgs e)
{
string text = TextBox3.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
Clipboard.SetText(text);
MessageBox.Show("路径已复制。", "复制成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Button7_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog
{
Filter = "JSON|*.json|TXT|*.txt|ALL|*.*"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
RichTextBox1.Text = File.ReadAllText(dialog.FileName);
MessageBox.Show("打开文件成功。", "打开成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
RichTextBox1.Text = string.Empty;
MessageBox.Show("打开文件失败。", "打开失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Button8_Click(object sender, EventArgs e)
{
string text = RichTextBox1.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
SaveFileDialog sfd = new SaveFileDialog
{
Filter = "JSON|*.json"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
File.WriteAllText(sfd.FileName, text);
MessageBox.Show("保存文件成功。", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("保存文件失败。", "保存失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}