JSON中JObject和JArray的修改

时间:2021-08-24 09:16:57
一、JObject 和JArray的添加、修改、移除

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
string json = @"{
'post':{
'Title':'修改JArray和JObject',
'Link':'http://write.blog.csdn.net',
'Description':'这是一个修改JArray和JObject的演示案例',
'Item':[]
}
}";

JObject o = JObject.Parse(json);
JObject post = (JObject)o["post"];

post["Title"] = ((string)post["Title"]).ToUpper();
post["Link"] = ((string)post["Link"]).ToUpper();

post.Property("Description").Remove();

post.Property("Link").AddAfterSelf(new JProperty("New", "新添加的属性"));

JArray a = (JArray)post["Item"];
a.Add("修改JArray");
a.Add("修改JObject");

Console.WriteLine(o.ToString());
}
}
}


2.运行的结果

JSON中JObject和JArray的修改

 

JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751