JSON中JObject和JArray的修改

一、JObject 和JArray的添加、修改、移除

1.先添加一个json字符串,把json字符串加载到JObject中,然后转换成JObject.根据索引修改对象的属性值,移除属性,添加属性

 
 1 using System;  
 2 using System.Collections.Generic;  
 3 using System.Linq;  
 4 using System.Text;  
 5 using System.Data;  
 6 using System.Web;  
 7 using GongHuiNewtonsoft.Json.Linq;  
 8   
 9 namespace JSONDemo  
10 {  
11     class Program  
12     {  
13         static void Main(string[] args)  
14         {  
15             string json = @"{  
16                 'post':{  
17                     'Title':'修改JArray和JObject',  
18                     'Link':'http://write.blog.csdn.net',  
19                     'Description':'这是一个修改JArray和JObject的演示案例',  
20                     'Item':[]  
21                 }  
22             }";  
23   
24             JObject o = JObject.Parse(json);  
25             JObject post = (JObject)o["post"];  
26   
27             post["Title"] = ((string)post["Title"]).ToUpper();  
28             post["Link"] = ((string)post["Link"]).ToUpper();  
29   
30             post.Property("Description").Remove();  
31   
32             post.Property("Link").AddAfterSelf(new JProperty("New", "新添加的属性"));  
33   
34             JArray a = (JArray)post["Item"];  
35             a.Add("修改JArray");  
36             a.Add("修改JObject");  
37   
38             Console.WriteLine(o.ToString());  
39         }  
40     }  
41 }  

2.运行的结果

原文地址:https://www.cnblogs.com/yanglang/p/6909161.html