• C# 给Json添加Note


            ·        string jsStr = "{'Name':'Long','Age':1,'Like':{'A':'a','B':'b'}}";

                    JObject json = (JObject)JsonConvert.DeserializeObject(jsStr);

    ===========================json 第一层的操作=========================

                    if (json.SelectToken("AA") == null)

                    {

                        json.First.AddAfterSelf(new JProperty("AA", "AA_value"));

                    // output: {{  "Name": "Long",  "AA": "AA_value",  "Age": 1,  "Like": {    "A": "a",    "B": "b"  }}} 

                    }

    ===============Json中存在对象时的操作=================

                    string tokenStr = "{\"C_key\":\"C_value\"}";

                    JObject jobject = (JObject)JsonConvert.DeserializeObject(tokenStr);

                    JToken jt = jobject as JToken;

                    json["Like"].First.AddAfterSelf(new JProperty("C", jt));

       //output: {{  "Name": "Long",  "AA": "AA_value",  "Age": 1,  "Like": {    "A": "a",    "C": {      "C_key": "C_value"    },    "B": "b"  }}}

    ===================修改第一层中节点内容=========================

    json["Name"] = "Ming";

    //json.Property("Name").Value = "Ming";  也可以修改,但是当出现多层时,就不再生效

    //output: {{  "Name": "Ming",  "AA": "AA_value",  "Age": 1,  "Like": {    "A": "A",    "B": "b"  }}}

    =================修改第二层中对象节点内容===================

    json["Like"]["A"] = "AAAA";

    // output: {{  "Name": "Ming",  "AA": "AA_value",  "Age": 1,  "Like": {    "A": "AAAA",    "B": "b"  }}}

    以下实例为修改chrome浏览器的download和popups属性

    public void UpdateChromeBrowserPreferenceFile()

            {

                try

                {

                    string path = "C:\\Users\\xxx\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences";

                    string fileText = File.ReadAllText(path, Encoding.UTF8); //"{  'Name': 'Long',  'profile': {     },  'download': {   },  'Age': 1,  'Like': {    'A': 'a',    'B': 'b'  }}";  //

                    JObject json = (JObject)JsonConvert.DeserializeObject(fileText);

                    //string downloadTokenStr = "{\"prompt_for_download\":true}";

                    //JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(downloadTokenStr);

                    //JToken jdt = jdTokeStr as JToken;

                    // set Ask where to save each file before downloading

                    if (json.SelectToken("download") == null)

                    {

                        string downloadTokenStr = "{\"prompt_for_download\":true}";

                        JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(downloadTokenStr);

                        JToken jdt = jdTokeStr as JToken;

                        json.First.AddAfterSelf(new JProperty("download", jdt));

                    }

                    else

                    {

                        //json.SelectToken("Like").SelectToken("A")

                        //prompt_for_download

                        if (json.SelectToken("download").SelectToken("prompt_for_download") == null)

                        {

                            if (json.SelectToken("download").Children().Count() == 0)

                            {

                                json.Property("download").Remove();

                                string downloadTokenStr = "{\"prompt_for_download\":true}";

                                JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(downloadTokenStr);

                                JToken jdt = jdTokeStr as JToken;

                                json.First.AddAfterSelf(new JProperty("download", jdt));

                            }

                            else

                            {

                                json["download"].First.AddAfterSelf(new JProperty("prompt_for_download", true));

                            }

                        }

                        else

                        {

                            json["download"]["prompt_for_download"] = true;

                        }

                    }

                    //Sites might send pop-ups to show ads, or use redirects to lead you to websites you may not want to visit

                    //if not have profile note

                    if (json.SelectToken("profile") == null)

                    {

                        string profileTokenStr = "{\"default_content_setting_values\":{\"popups\":1}}";

                        JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(profileTokenStr);

                        JToken jdt = jdTokeStr as JToken;

                        json.First.AddAfterSelf(new JProperty("profile", jdt));

                    }

                    //if have profile note

                    else

                    {

                        // if not have default_content_setting_values under the profile note

                        if (json.SelectToken("profile").SelectToken("default_content_setting_values") == null)

                        {

                            //if dont have any note under the profile note

                            if (json.SelectToken("profile").Children().Count() == 0)

                            {

                                json.Property("profile").Remove();

                                string profileTokenStr = "{\"default_content_setting_values\":{\"popups\":1}}";

                                JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(profileTokenStr);

                                JToken jdt = jdTokeStr as JToken;

                                json.First.AddAfterSelf(new JProperty("profile", jdt));

                            }

                            else

                            {

                                string popupsTokenStr = "{\"popups\":1}";

                                JObject jdTokeStr = (JObject)JsonConvert.DeserializeObject(popupsTokenStr);

                                JToken jdt = jdTokeStr as JToken;

                                json["profile"].First.AddAfterSelf(new JProperty("default_content_setting_values", jdt));

                            }

                        }

                        // if have default_content_setting_values under the profile note

                        else

                        {

                            //if not have popups under the proflet/default_content_setting_values note

                            if (json.SelectToken("profile").SelectToken("default_content_setting_values").SelectToken("popups") == null)

                            {

                                json["profile"]["default_content_setting_values"].First.AddAfterSelf(new JProperty("popups", 1));

                            }

                            else

                            {

                                json["profile"]["default_content_setting_values"]["popups"] = 1;

                            }

                        }

                    }

                    System.IO.File.WriteAllText("C:\\lming\\Work\\200\\Preferences_C", json.ToString(), Encoding.UTF8);

                    Console.WriteLine("aa");

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message);

                    throw ex;

                }

            }

  • 相关阅读:
    围绕“开源+深耕”策略和数字化监控手段,动态管理场景生态价值
    Spring Boot + vue-element 开发个人博客项目实战教程(二十四、文章管理页面开发(3))
    怎么提取pdf格式中的英语单词
    央媒发稿不能改?媒体发布新闻稿有哪些注意点
    js中的拖拽
    视频拍摄教程分享
    Servlet---ServletConfig类使用介绍
    [C++基础]-类和对象(中~万字详解)
    IGI60F1414A1L功率级 IC,ISO7640FMDW数字隔离器 特点
    Lake Shore M91快速霍尔测量仪
  • 原文地址:https://blog.csdn.net/u011187194/article/details/128103017