· 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;
}
}
上一篇: 绽放的花朵初中作文(精彩6篇)