php下json解析工具,JSON解析工具

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);

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值