-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONExplorer.cs
More file actions
175 lines (160 loc) · 6.36 KB
/
Copy pathJSONExplorer.cs
File metadata and controls
175 lines (160 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using Aspose.Cells;
using Aspose.Cells.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace TableLookUp
{
public partial class JSONWindow : Form
{
private TextBox jsonContainer = new TextBox();
private string rootName;
public JSONWindow(JArray noParents, string dbName, int total, string elapsed)
{
InitializeComponent();
labTotal.Text += total;
labTime.Text += elapsed;
rootName = dbName;
Text = "Base: " + dbName;
BuildTree(noParents);
}
private void BuildTree(JArray tableNodes)
{
JObject obj = new JObject {
{ "tables", tableNodes }
};
jsonExplorer.Nodes.Clear();
jsonContainer.Text = obj.ToString();
TreeNode n = Json2Tree(obj);
n.Text = rootName;
jsonExplorer.Nodes.Add(n);
}
private TreeNode Json2Tree(JObject obj)
{
//create the parent node
TreeNode parent = new TreeNode();
//loop through the obj. all token should be pair<key, value>
foreach (var token in obj)
{
//change the display Content of the parent
//parent.Text = token.Key.ToString();
//create the child node
TreeNode child = new TreeNode();
child.Text = token.Key.ToString();
//check if the value is of type obj recall the method
if (token.Value.Type.ToString() == "Object")
{
// child.Text = token.Key.ToString();
//create a new JObject using the the Token.value
JObject o = (JObject)token.Value;
//recall the method
child = Json2Tree(o);
//add the child to the parentNode
parent.Nodes.Add(child);
}
//if type is of array
else if (token.Value.Type.ToString() == "Array")
{
int ix = -1;
// child.Text = token.Key.ToString();
//loop though the array
foreach (var itm in token.Value)
{
//check if value is an Array of objects
if (itm.Type.ToString() == "Object")
{
TreeNode objTN = new TreeNode();
//child.Text = token.Key.ToString();
//call back the method
ix++;
JObject o = (JObject)itm;
objTN = Json2Tree(o);
objTN.Text = token.Key.ToString() + "[" + ix + "]";
child.Nodes.Add(objTN);
//parent.Nodes.Add(child);
}
//regular array string, int, etc
else if (itm.Type.ToString() == "Array")
{
ix++;
TreeNode dataArray = new TreeNode();
foreach (var data in itm)
{
dataArray.Text = token.Key.ToString() + "[" + ix + "]";
dataArray.Nodes.Add(data.ToString());
}
child.Nodes.Add(dataArray);
}
else
{
child.Nodes.Add(itm.ToString());
}
}
parent.Nodes.Add(child);
}
else
{
//if token.Value is not nested
// child.Text = token.Key.ToString();
//change the value into N/A if value == null or an empty string
if (token.Value.ToString() == "")
child.Nodes.Add("N/A");
else
child.Nodes.Add(token.Value.ToString());
parent.Nodes.Add(child);
}
}
return parent;
}
private void excel_Click(object sender, EventArgs e)
{
saveExcel.Filter = "Excel Files | *.xlsx";
saveExcel.ShowDialog();
}
private void jsonExport_Click(object sender, EventArgs e)
{
saveJson.Filter = "Json Files | *.json";
saveJson.ShowDialog();
}
private void saveFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string name = saveJson.FileName;
JArray json = JArray.Parse("[" + jsonContainer.Text + "]");
using (StreamWriter file = File.CreateText(name))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
json.WriteTo(writer);
}
}
private void saveExcel_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string name = saveExcel.FileName;
// Get JSON text
JObject obj = JObject.Parse(jsonContainer.Text);
obj.AddFirst(new JProperty("name", rootName));
string jsonString = "[" + obj.ToString() + "]";
// Create a Workbook object
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Set Styles
CellsFactory factory = new CellsFactory();
Style style = factory.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.Font.Color = System.Drawing.Color.BlueViolet;
style.Font.IsBold = true;
// Set JsonLayoutOptions
JsonLayoutOptions options = new JsonLayoutOptions();
options.ConvertNumericOrDate = true;
options.TitleStyle = style;
options.ArrayAsTable = true;
// Import JSON Data
JsonUtility.ImportData(jsonString, worksheet.Cells, 0, 0, options);
// Save Excel file
workbook.Save(@name);
}
}
}