首页 > 编程语言 >[原创]C# JSON节点的替换整体套路

[原创]C# JSON节点的替换整体套路

时间:2022-11-24 13:08:21浏览次数:45  
标签:Newtonsoft zone C# JObject Json Linq 套路 JSON location


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);
}

标签:Newtonsoft,zone,C#,JObject,Json,Linq,套路,JSON,location
From: https://blog.51cto.com/u_15458814/5883442

相关文章

  • 坑爹奇葩的jdbc getColumnName(index)竟然从1开始
    /***Getthedesignatedcolumn'sname.**@paramcolumnthefirstcolumnis1,thesecondis2,...*@returncolumnname*@exceptionSQL......
  • 减少C盘空间计划记录
    前言**按照我这个操作了之后你的c盘不但减轻了负担,也不怕以后重装系统某些东西丢失了操作1.更改我的文档,我的视频,我的图片桌面的位置为d盘2.更改gradle默认路径设置环境......
  • ida incompatible debugging server:debugger id is 1 expected 11
    image.png场景:调试夜神模拟器选择调试为androiddebugger,结果出现了这个错误​​incompatibledebuggingserver:debuggeridis1expected11​​因此改成lin......
  • 玩转java bytecode第一篇从java到asm的转换。
    java源码publicclassSimpleMain{publicstaticvoidmain(String[]args){System.out.println("333:");}}javap反汇编​​javap-p-c-s-lTestAsmTra......
  • 原创 andrid8.0以及以上获取顶层任务中activity失效的解决方法
    1/某些activity手机端都未必能获取,手机端一般是使用的无障碍服务实现的,2/8.0以前的命令find已经无法找到了。C:\Users\Administrator>adbshelldumpsysactivity|finds......
  • eclipse android ndk相关
    配置NDK-BUNDLE​​D:\env\android_sdk\ndk-bundle\build​​image.png配置路径和符号​​D:\env\android_sdk\ndk-bundle\toolchains\aarch64-linux-android-4.......
  • c#按键的拖拽模板代码
    publicvoidBtnMove(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){if(!mAllowDragKey)......
  • c#转义的缺陷
    stringjson1="斜杠转义\\这个不需要转义{}双引号转义\"f单引号不需要转义'f";stringjson2=@"反斜杠转义1\双引号不行的";string@stri......
  • 【Linux】PKG_CONFIG_PATH
    【Linux】PKG_CONFIG_PATHyepoyou于 2020-11-1319:05:29 发布1941 收藏 7分类专栏: Linux 文章标签: linux版权Linux专栏收录该内容7篇文章0......
  • c#自定义控件模板代码
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSyste......