<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Json閱讀小工具制作

      由于任務(wù)需要,需要對僅僅三個字段的Json文件進(jìn)行閱讀。這里制作了一個簡易版的Json閱讀工具,通過字段的遍歷獲取一個小工具,用來實現(xiàn)Json的可視化閱讀

      直接上代碼

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.IO;
      using System.Linq;
      using System.Text;
      using System.Text.Json;
      using System.Text.Json.Nodes;
      using System.Windows.Forms;
      
      namespace JsonFormsTool
      {
          public partial class Form1 : Form
          {
              private JsonNode currentJson;
              private List<TreeNode> searchResults = new List<TreeNode>();
              private int currentSearchIndex = -1;
      
              public Form1()
              {
                  InitializeComponent();
                  InitializeEventHandlers();
                  this.Text = "Json查看工具";
              }
      
              private void InitializeEventHandlers()
              {
                  // 打開文件按鈕事件
                  toolStripButton1.Click += ToolStripButton1_Click;
      
                  // 搜索按鈕事件
                  toolStripButton2.Click += ToolStripButton2_Click;
      
                  // 上一個搜索結(jié)果按鈕事件
                  toolStripButton3.Click += ToolStripButton3_Click;
      
                  // 下一個搜索結(jié)果按鈕事件
                  toolStripButton4.Click += ToolStripButton4_Click;
      
                  // 樹形視圖節(jié)點點擊事件
                  treeView1.AfterSelect += TreeView1_AfterSelect;
      
                  // 搜索框按鍵事件
                  toolStripTextBox1.KeyDown += ToolStripTextBox1_KeyDown;
              }
      
              private void ToolStripButton1_Click(object sender, EventArgs e)
              {
                  using (OpenFileDialog openFileDialog = new OpenFileDialog())
                  {
                      openFileDialog.Filter = "Json文件 (*.json)|*.json|所有文件 (*.*)|*.*";
                      openFileDialog.Title = "打開Json文件";
      
                      if (openFileDialog.ShowDialog() == DialogResult.OK)
                      {
                          try
                          {
                              string filePath = openFileDialog.FileName;
                              string jsonContent = File.ReadAllText(filePath);
      
                              // 在RichTextBox中顯示全文
                              richTextBox1.Text = jsonContent;
      
                              // 解析并顯示到TreeView
                              ParseAndDisplayJson(jsonContent);
      
                              // 展開所有節(jié)點
                              treeView1.ExpandAll();
      
                              // 清空搜索結(jié)果
                              searchResults.Clear();
                              currentSearchIndex = -1;
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show($"打開文件時出錯: {ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                          }
                      }
                  }
              }
      
              private void ParseAndDisplayJson(string jsonContent)
              {
                  try
                  {
                      // 解析JSON
                      currentJson = JsonNode.Parse(jsonContent);
                      if (currentJson == null)
                      {
                          MessageBox.Show("無法解析JSON內(nèi)容", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                          return;
                      }
      
                      // 清空TreeView
                      treeView1.Nodes.Clear();
      
                      // 創(chuàng)建根節(jié)點
                      TreeNode rootNode;
                      if (currentJson is JsonObject)
                      {
                          rootNode = new TreeNode("根對象");
                          rootNode.Tag = currentJson;
                          treeView1.Nodes.Add(rootNode);
                          AddJsonObjectToTree((JsonObject)currentJson, rootNode);
                      }
                      else if (currentJson is JsonArray)
                      {
                          rootNode = new TreeNode("根數(shù)組");
                          rootNode.Tag = currentJson;
                          treeView1.Nodes.Add(rootNode);
                          AddJsonArrayToTree((JsonArray)currentJson, rootNode);
                      }
                      else
                      {
                          rootNode = new TreeNode($"值: {currentJson.ToString()}");
                          rootNode.Tag = currentJson;
                          treeView1.Nodes.Add(rootNode);
                      }
      
                      // 默認(rèn)選擇第一個節(jié)點
                      if (treeView1.Nodes.Count > 0)
                      {
                          treeView1.SelectedNode = treeView1.Nodes[0];
                      }
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show($"解析JSON時出錯: {ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }
              }
      
              private void AddJsonObjectToTree(JsonObject jsonObject, TreeNode parentNode)
              {
                  foreach (var property in jsonObject)
                  {
                      TreeNode node = new TreeNode(property.Key);
                      node.Tag = property.Value;
      
                      if (property.Value is JsonObject)
                      {
                          node.Text += " : 對象";
                          AddJsonObjectToTree((JsonObject)property.Value, node);
                      }
                      else if (property.Value is JsonArray)
                      {
                          node.Text += $" : 數(shù)組[{((JsonArray)property.Value).Count}]";
                          AddJsonArrayToTree((JsonArray)property.Value, node);
                      }
                      else
                      {
                          string value = property.Value?.ToString() ?? "null";
                          if (value.Length > 50)
                              value = value.Substring(0, 50) + "...";
                          node.Text += $" : {value}";
                      }
      
                      parentNode.Nodes.Add(node);
                  }
              }
      
              private void AddJsonArrayToTree(JsonArray jsonArray, TreeNode parentNode)
              {
                  for (int i = 0; i < jsonArray.Count; i++)
                  {
                      TreeNode node = new TreeNode($"[{i}]");
                      node.Tag = jsonArray[i];
      
                      if (jsonArray[i] is JsonObject)
                      {
                          node.Text += " : 對象";
                          AddJsonObjectToTree((JsonObject)jsonArray[i], node);
                      }
                      else if (jsonArray[i] is JsonArray)
                      {
                          node.Text += $" : 數(shù)組[{((JsonArray)jsonArray[i]).Count}]";
                          AddJsonArrayToTree((JsonArray)jsonArray[i], node);
                      }
                      else
                      {
                          string value = jsonArray[i]?.ToString() ?? "null";
                          if (value.Length > 50)
                              value = value.Substring(0, 50) + "...";
                          node.Text += $" : {value}";
                      }
      
                      parentNode.Nodes.Add(node);
                  }
              }
      
              private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
              {
                  if (e.Node.Tag is JsonNode jsonNode)
                  {
                      // 格式化JSON節(jié)點內(nèi)容并顯示
                      string jsonString = jsonNode.ToJsonString(new JsonSerializerOptions
                      {
                          WriteIndented = true,
                          Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                      });
      
                      // 處理換行符
                      jsonString = jsonString.Replace("\\n", "\n");
      
                      richTextBox1.Text = jsonString;
                  }
              }
      
              private void SearchJsonTree(string searchText)
              {
                  if (string.IsNullOrWhiteSpace(searchText))
                      return;
      
                  searchResults.Clear();
                  currentSearchIndex = -1;
      
                  // 遞歸搜索所有節(jié)點
                  SearchNodes(treeView1.Nodes, searchText);
      
                  if (searchResults.Count > 0)
                  {
                      currentSearchIndex = 0;
                      treeView1.SelectedNode = searchResults[0];
                      treeView1.SelectedNode.BackColor = Color.Yellow;
                      treeView1.SelectedNode.EnsureVisible();
                  }
                  else
                  {
                      MessageBox.Show("未找到匹配項", "搜索", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  }
              }
      
              private void SearchNodes(TreeNodeCollection nodes, string searchText)
              {
                  foreach (TreeNode node in nodes)
                  {
                      if (node.Text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
                      {
                          searchResults.Add(node);
                      }
      
                      SearchNodes(node.Nodes, searchText);
                  }
              }
      
              private void HighlightSearchResult()
              {
                  // 清除之前的高亮
                  foreach (TreeNode node in searchResults)
                  {
                      node.BackColor = treeView1.BackColor;
                  }
      
                  // 高亮當(dāng)前結(jié)果
                  if (currentSearchIndex >= 0 && currentSearchIndex < searchResults.Count)
                  {
                      treeView1.SelectedNode = searchResults[currentSearchIndex];
                      searchResults[currentSearchIndex].BackColor = Color.Yellow;
                      searchResults[currentSearchIndex].EnsureVisible();
                  }
              }
      
              private void ToolStripButton2_Click(object sender, EventArgs e)
              {
                  SearchJsonTree(toolStripTextBox1.Text);
              }
      
              private void ToolStripButton3_Click(object sender, EventArgs e)
              {
                  if (searchResults.Count > 0)
                  {
                      currentSearchIndex = (currentSearchIndex - 1 + searchResults.Count) % searchResults.Count;
                      HighlightSearchResult();
                  }
              }
      
              private void ToolStripButton4_Click(object sender, EventArgs e)
              {
                  if (searchResults.Count > 0)
                  {
                      currentSearchIndex = (currentSearchIndex + 1) % searchResults.Count;
                      HighlightSearchResult();
                  }
              }
      
              private void ToolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
              {
                  if (e.KeyCode == Keys.Enter)
                  {
                      SearchJsonTree(toolStripTextBox1.Text);
                      e.Handled = true;
                      e.SuppressKeyPress = true;
                  }
              }
          }
      }
      

      顯示效果:

      這里支持搜索

      另外附上可直接運行版本:
      https://wwus.lanzouu.com/iJ0Xt2yms9kb
      密碼:9664

      posted @ 2025-06-12 14:57  嘻嘻哈哈555  閱讀(47)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产精品成人一区二区三区| 午夜av高清在线观看| 国产欧美亚洲精品第1页| 在线无码免费的毛片视频| 毛葺葺老太做受视频| 国内精品久久久久影院网站| 无码人妻精品一区二区三区下载| 欧美成本人视频免费播放| 国产午夜福利精品片久久| 草草浮力影院| 丰满人妻被黑人猛烈进入| 小污女小欲女导航| 久热这里只有精品12| 亚洲精品无码久久千人斩| 精品人妻伦九区久久69| 国产亚洲精品AA片在线爽| 风韵丰满熟妇啪啪区老熟熟女| 亚洲AV旡码高清在线观看| 久久青青草原亚洲AV无码麻豆| 洪泽县| 久久国产成人av蜜臀| 精品九九人人做人人爱| 人妻饥渴偷公乱中文字幕| 欧美亚洲日本国产其他| 午夜不卡久久精品无码免费| 欧美和黑人xxxx猛交视频| 四虎影视一区二区精品| 久久日产一线二线三线| 日韩精品成人网页视频在线 | 亚洲国产精品一区在线看| 色噜噜噜亚洲男人的天堂| 好紧好爽午夜视频| 国产在线视频导航| 宁津县| 国产口爆吞精在线视频2020版 | 国产成人AV性色在线影院| 久久热这里只有精品66| 亚洲中文字幕一二三四区| 久久av无码精品人妻系列试探| 亚洲国产精品久久久天堂麻豆宅男| 博乐市|