internal static JsonTreeNode CreateNode(string property, JToken item) { NodeType type; string text; string textWhenSelected = null; if (item.Type == JTokenType.Object || item.Type == JTokenType.Array) { text = property; textWhenSelected = text; } else { //return null; type = NodeType.Value; string value = item.ToString(); text = property == null ? value : string.Format($"{property}: {value}"); textWhenSelected = string.Format($"{text} (type: {item.Type})"); } type = item.Type == JTokenType.Object ? NodeType.Object : item.Type == JTokenType.Array ? NodeType.Array : NodeType.Value; var node = new JsonTreeNode(type, text, textWhenSelected); node.ImageKey = item.Type.ToString(); node.SelectedImageKey = node.ImageKey; return(node); }
private void LoadObject(JObject jobject, JsonTreeNode rootNode, Flag flag) { foreach (var item in jobject) { if (item.Key != "property" && item.Key != "method") { AddNode(rootNode, item.Key, item.Value, flag); } switch (flag) { case Flag.OnlyObject: if (JasonKeyValue.Count(x => x.Key == item.Key) == 0) { if (JToken.FromObject((object)item.Value).Type == JTokenType.Object || JToken.FromObject((object)item.Value).Type == JTokenType.Array) { JasonKeyValue.Add(item.Key, item.Value); } } break; case Flag.All: if (JasonKeyValue.Count(x => x.Key == item.Key) == 0) { JasonKeyValue.Add(item.Key, item.Value); } break; } } }
private void AddNode(JsonTreeNode parentNode, string property, JToken item, Flag flag) { JsonTreeNode node = null; if (flag == Flag.All) { node = JsonTreeNodeCreator.CreateNode(property, item); parentNode.Nodes.Add(node); } else { if (item.Type == JTokenType.Object || item.Type == JTokenType.Array) { node = JsonTreeNodeCreator.CreateNode(property, item); parentNode.Nodes.Add(node); } } if (item.Type == JTokenType.Array) { LoadArray(item, node, flag); } if (item.Type == JTokenType.Object) { LoadObject(item as JObject, node, flag); } }
//public void UpdateJasonObject(string key, string value) //{ // Dictionary<string, object> father = null; // foreach (var item in JsonRootList) // { // if (key == item) // { // JasonKeyValue[item] = value; // } // else // { // Dictionary<string, object> tempdic = JsonConvert.DeserializeObject<Dictionary<string, object>>(JasonKeyValue[item]); // if (tempdic.Count(x => x.Key == key) > 0) // { // tempdic[key] = value;//二层循环判断。 // } // JasonKeyValue[item] = JsonConvert.SerializeObject(tempdic); // for (int i = 0; i < JsonRootList.Count - 1; i++) // { // father = JsonConvert.DeserializeObject<Dictionary<string, object>>(JasonKeyValue[JsonRootList[i + 1]]); // string fatherName = JsonRootList[i + 1]; // dynamic child = JsonConvert.DeserializeObject<dynamic>(JasonKeyValue[JsonRootList[i]]); // father[JsonRootList[i]] = child; // JasonKeyValue[fatherName] = JsonConvert.SerializeObject(father); // } // } // } // File.WriteAllText(Application.StartupPath + "/Out.json", JsonConvert.SerializeObject(father)); // nowJson = JsonConvert.SerializeObject(father); // this.needbeencode?.Invoke(); // this.NodeClick?.Invoke(JsonConvert.SerializeObject(JasonKeyValue[key].ToString())); //} /// <summary> /// 从JSON字符串中获取TreeView节点数 /// </summary> /// <param name="parentId"></param> /// <param name="jsonstr"></param> /// <param name="flag"></param> public bool GetRootFromJsonStr(int parentId, string jsonstr, Flag flag) { try { JasonKeyValue.Clear(); JObject jobject = JObject.Parse(jsonstr); var rootNode = new JsonTreeNode(NodeType.Object, "root"); //TreeView tw = new TreeView(); //tw.Nodes.Add(rootNode); //tw.SelectedNode = rootNode; rootNode.ImageKey = rootNode.NodeType.ToString(); rootNode.SelectedImageKey = rootNode.ImageKey; LoadObject(jobject, rootNode, flag); rootNode.Expand(); TreeNode = rootNode; return(true); } catch (Exception ex) { return(false); } //TreeView = tw; //TreeView.NodeMouseDoubleClick += TreeView_NodeMouseDoubleClick; //TreeView.NodeMouseClick += TreeView_NodeMouseClick; }
private void LoadArray(JToken item, JsonTreeNode node, Flag flag) { int i = 0; foreach (var childitem in item) { AddNode(node, i++.ToString(), childitem, flag); //JasonKeyValue.Add(seed, childitem); } }