private static void replaceSelfJSONFromChild()
{
string groupJSON = "{\"zone\":\"海淀\",\"zone_en\":\"haidian\",'location':{x:1,y:2}}";
Newtonsoft.Json.Linq.JObject groupJSONObject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(groupJSON);
JObject locationObject=(JObject)groupJSONObject.GetValue("location");
string replaceJSON = "{x:5,y:6}";
Newtonsoft.Json.Linq.JObject willReplaceJSON = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(replaceJSON);
locationObject.RemoveAll();
locationObject.Merge(willReplaceJSON);
Console.WriteLine("JSON:" + groupJSONObject);
}
private static void replaceSelfJSON()
{
string jsonText = "{\"zone\":\"海淀\",\"zone_en\":\"haidian\"}";
Newtonsoft.Json.Linq.JObject jo = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
string jsonText111 = "{\"zone\":\"海淀333\",\"zone_en\":\"haidian\"}";
Newtonsoft.Json.Linq.JObject jo1 = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText111);
jo.RemoveAll();
jo.Merge(jo1);
Console.WriteLine("JSON:" + jo);
}
private static void removeSelfChildJSON()
{
string groupJSON = "{\"zone\":\"海淀\",\"zone_en\":\"haidian\",'location':{x:1,y:2}}";
Newtonsoft.Json.Linq.JObject groupJSONObject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(groupJSON);
JObject locationObject = (JObject)groupJSONObject.GetValue("location");
//locationObject.RemoveAll();
((JObject)locationObject.Parent.Parent).Remove("location");
//locationObject.Merge(willReplaceJSON);
Console.WriteLine("JSON:" + groupJSONObject);
}
private static void removeSelfChildArray()
{
string groupJSON = "[" +
"{ 'name':'luo','y':2,'location':{z:1,n:5} } " +
",{ 'name':'zheng','y':2,'location':{z:1,n:5} } " +
",{ 'name':'ye','y':2,'location':{z:1,n:5} } " +
"]";
Newtonsoft.Json.Linq.JArray jarray = (Newtonsoft.Json.Linq.JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(groupJSON);
JObject jobject = (JObject)jarray[0];
//jobject.Remove();
//((JArray)jobject.Parent).Remove(jobject);
((JArray)jobject.Parent).RemoveAt(0);
//locationObject.RemoveAll();
Console.WriteLine("JSON:" + jobject + ",removeAfter" + jarray);
}
[原创]C# JSON节点的替换整体套路
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 关于就事论事探讨很多,并思考了不少,但由于抽象思维能力不足,具象思维能力有鱼余,总不能够把它想透,想全想开。 小王...
- 1、自定义节点(字体颜色、图标等) 2、实现不同级别树节点的背景颜色自定义 如果再采用自定义节点的方式来修改背景颜...
- 比如一本书有关的结构体 会有针对书整体,和针对书的每一个章节这样的结构体。 我们在章节里定义一个 book的指针,...
- 删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点。如删除成功,此方法...