json解析程序源代码
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace json解析
{
public partial class json解析 : Form
{
public json解析()
{
InitializeComponent();
}
#region 事件
//打开文件
private void btn_openFile_Click(object sender, EventArgs e)
{
string filePath = “”;
OpenFileDialog opfile = new OpenFileDialog();
if (opfile.ShowDialog() == DialogResult.OK)
{
filePath = opfile.FileName;
}
if (filePath == “” || filePath==null)
{
return;
}
string fileData = getFileData(filePath);
rtxt_json.Text = fileData;
BindTreeView(tv_json, rtxt_json.Text.ToString ());
}
//直接输入
private void btn_inputData_Click(object sender, EventArgs e)
{
rtxt_json.Text = “”;
}
//解析
private void btn_analyze_Click(object sender, EventArgs e)
{
BindTreeView(tv_json, rtxt_json.Text.ToString());
}
//全部展开
private void btn_getAll_Click(object sender, EventArgs e)
{
if(tv_json .Nodes.Count>0)
{
tv_json.ExpandAll();
}
}
//收起全部
private void btn_getSmall_Click(object sender, EventArgs e)
{
if (tv_json.Nodes.Count > 0)
{
tv_json.CollapseAll();
}
}
//treeview的宽度
private void json解析_SizeChanged(object sender, EventArgs e)
{
tv_json.Width = skinPanel2.Width / 2;
}
#endregion
#region 核心代码
/// <summary>
/// 读取txt文件内容
/// </summary>
/// <param name=”path”></param>
/// <returns></returns>
private string getFileData(string path)
{
string strData = “”;
StreamReader sr = new StreamReader(path, Encoding.GetEncoding(“gb2312”));
strData = sr.ReadToEnd();
return strData;
}
/// <summary>
/// 绑定树形控件
/// </summary>
/// <param name=”treeView”></param>
/// <param name=”strJson”></param>
private void BindTreeView(TreeView treeView, string strJson)
{
treeView.Nodes.Clear();
//string strJson = “1”;//抛异常
//string strJson = “{}”;
//string strJson = “{\”errcode\”:0,\”errmsg\”:\”查询成功\”,\”datas\”:[{\”c_ResourceType\”:\”BootLogo\”,\”c_Url\”:\”/Upload/Magazine/4e09315d-7d92-4e6a-984d-80f684a24da8.jpg\”}]}”;
//string strJson = “[{\”DeviceCode\”:\”430BE-B3C6A-4E953-9F972-FC741\”,\”RoomNum\”:\”777\”},{\”DeviceCode\”:\”BF79F -09807-EEA31-2499E-31A98\”,\”RoomNum\”:\”888\”}]”;
if (IsJOjbect(strJson))
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);
foreach (var item in jo)
{
TreeNode tree;
if (item.Value.GetType() == typeof(JObject))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
treeView.Nodes.Add(tree);
}
else if (item.Value.GetType() == typeof(JArray))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
treeView.Nodes.Add(tree);
}
else
{
tree = new TreeNode(item.Key + “:” + item.Value.ToString());
treeView.Nodes.Add(tree);
}
}
}
if (IsJArray(strJson))
{
JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
int i = 0;
foreach (JObject item in ja)
{
TreeNode tree = new TreeNode(“Array [” + (i++) + “]”);
foreach (var itemOb in item)
{
TreeNode treeOb;
if (itemOb.Value.GetType() == typeof(JObject))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else if (itemOb.Value.GetType() == typeof(JArray))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else
{
treeOb = new TreeNode(itemOb.Key + “:” + itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
}
treeView.Nodes.Add(tree);
}
}
treeView.ExpandAll();
}
/// <summary>
/// 添加子节点
/// </summary>
/// <param name=”parantNode”></param>
/// <param name=”value”></param>
public void AddTreeChildNode(ref TreeNode parantNode, string value)
{
if (IsJOjbect(value))
{
JObject jo = (JObject)JsonConvert.DeserializeObject(value);
foreach (var item in jo)
{
TreeNode tree;
if (item.Value.GetType() == typeof(JObject))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
parantNode.Nodes.Add(tree);
}
else if (item.Value.GetType() == typeof(JArray))
{
tree = new TreeNode(item.Key);
AddTreeChildNode(ref tree, item.Value.ToString());
parantNode.Nodes.Add(tree);
}
else
{
tree = new TreeNode(item.Key + “:” + item.Value.ToString());
parantNode.Nodes.Add(tree);
}
}
}
if (IsJArray(value))
{
JArray ja = (JArray)JsonConvert.DeserializeObject(value);
int i = 0;
foreach (JObject item in ja)
{
TreeNode tree = new TreeNode(“Array [” + (i++) + “]”);
parantNode.Nodes.Add(tree);
foreach (var itemOb in item)
{
TreeNode treeOb;
if (itemOb.Value.GetType() == typeof(JObject))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else if (itemOb.Value.GetType() == typeof(JArray))
{
treeOb = new TreeNode(itemOb.Key);
AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
else
{
treeOb = new TreeNode(itemOb.Key + “:” + itemOb.Value.ToString());
tree.Nodes.Add(treeOb);
}
}
}
}
}
/// <summary>
/// 判断是否JOjbect类型
/// </summary>
/// <param name=”value”></param>
/// <returns></returns>
private bool IsJOjbect(string value)
{
try
{
JObject ja = JObject.Parse(value);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 判断是否JArray类型
/// </summary>
/// <param name=”value”></param>
/// <returns></returns>
private bool IsJArray(string value)
{
try
{
JArray ja = JArray.Parse(value);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}
}