irpas技术客

C# 给Json添加Note_c# json 追加_小明不知上班的路

网络 6836

? ? ? ? ·????????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;

? ? ? ? ? ? }

? ? ? ? }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #C #JSON #追加