其他分享
首页 > 其他分享> > Newtonsoft.Json 编辑Json

Newtonsoft.Json 编辑Json

作者:互联网

Newtonsoft.Json官网 Json.NET - Newtonsoft

 

文档地址Introduction (newtonsoft.com)

 

示例Modifying JSON (newtonsoft.com)

string json = @"{
  'channel': {
    'title': 'Star Wars',
    'link': 'http://www.starwars.com',
    'description': 'Star Wars blog.',
    'obsolete': 'Obsolete value',
    'item': []
  }
}";

JObject rss = JObject.Parse(json);

JObject channel = (JObject)rss["channel"];

channel["title"] = ((string)channel["title"]).ToUpper();
channel["description"] = ((string)channel["description"]).ToUpper();

channel.Property("obsolete").Remove();

channel.Property("description").AddAfterSelf(new JProperty("new", "New value"));

JArray item = (JArray)channel["item"];
item.Add("Item 1");
item.Add("Item 2");

Console.WriteLine(rss.ToString());
// {
//   "channel": {
//     "title": "STAR WARS",
//     "link": "http://www.starwars.com",
//     "description": "STAR WARS BLOG.",
//     "new": "New value",
//     "item": [
//       "Item 1",
//       "Item 2"
//     ]
//   }
// }

 

项目中使用,直接在VS中安装相应Nuget包——Newtonsoft.Json即可!

 

标签:Newtonsoft,description,item,JObject,编辑,Json,channel
来源: https://www.cnblogs.com/chengcanghai/p/16670455.html