zoukankan      html  css  js  c++  java
  • C#- JSON的操作

     读,删,修改JSON

                string jsonPath = System.Windows.Forms.Application.StartupPath + "\Data\probeData.json";
                string jsonString = File.ReadAllText(jsonPath);
    
                JObject obj;
                if (jsonString.Trim() == "")
                    obj = new JObject();
                else
                    obj = JObject.Parse(jsonString);
    
                JObject jArray = new JObject();
    
                JObject newObj = new JObject(
                        new JProperty("name", "5L64-0.8x10-C18"),
                        new JProperty("custom", 1.1),
                        new JProperty("freq", 2.1)
                    );
    
                JObject newObj2 = new JObject(
                        new JProperty("name", "5L64-0.8x10-C18"),
                        new JProperty("custom", 1.3),
                        new JProperty("freq", 2.2)
                    );
    
                jArray.Add("aaa4", newObj);
                jArray.Add("aaa6", newObj2);
    
    
                obj.Add("probe", jArray);
    
    
    
    
                //--------------------删除--------------------
                //JObject tokeselect = obj.SelectToken("probe") as JObject;
                //JObject tokeselect2 = tokeselect.SelectToken("aaa4") as JObject;
                ////tokeselect.Remove("aaa4");
                //tokeselect2.Remove("name");
                //--------------------删除--------------------
    
                //--------------------修改--------------------
                obj["probe"]["aaa4"]["name"] = "ccc123";
    
                jsonString = Convert.ToString(obj);
                System.Diagnostics.Trace.WriteLine(jsonString);

     遍历JSON

     string jsonPath = System.Windows.Forms.Application.StartupPath + "\global\probe.json";
                bool isExist = FileHelper.IsExistFile(jsonPath);
                JObject JObj;
                if (isExist)
                {
                    string jsonString = File.ReadAllText(jsonPath);
                    if (jsonString.Trim() == "")
                        JObj = new JObject();
                    else
                        JObj = JObject.Parse(jsonString);
    
                     string str = JObj["probe"].ToString();
    
                    //JArray jar = JArray.Parse(str);
                    //for (int i = 0; i < jar.Count; i++)
                    //{
                    //    JToken jt = JToken.Parse(jar[i].ToString());
                    //    foreach (JProperty jp in jt)
                    //    {
                    //        string name = jp.Name;
                    //    }
                    //}
    
    
                    JToken jt = JToken.Parse(str);
                    foreach (JProperty jp in jt)
                    {
                        string name = jp.Name;
                        JObject obj2 = JObject.Parse(jp.Value.ToString());
                        string xxx = obj2["name"].ToString();
                    }
    
                }

    JSON文件:

    {
      "probe": {
        "aaa4": {
          "name": "ccc123",
          "custom": "1",
          "freq": "23",
          "dist": "33",
          "delay": "11",
          "date": "2021-08-21"
        },
        "aaa6": {
          "name": "ccc111",
          "custom": "2",
          "freq": "13",
          "dist": "33",
          "delay": "31",
          "date": "2021-08-22"
        }
      }
    }

  • 相关阅读:
    JAVA中handleEvent和action的区别
    Hessian的使用以及理解
    Java基础中的RMI介绍与使用
    Callable与Runable接口 submit与execute区别
    XXL-JOB原理--定时任务框架简介(一)
    11.并发包阻塞队列之LinkedBlockingQueue
    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
    正确实现用spring扫描自定义的annotation
    自贡进入“刷脸卡”时代 人脸识别支付“黑科技”现身自流井老街
    谷歌最新研究:量子计算机能在8小时内破解2048位RSA加密
  • 原文地址:https://www.cnblogs.com/cxeye/p/15168888.html
Copyright © 2011-2022 走看看