Use JPath but not recursively loop a JObject to modify the values.
I am dealing with a Json file, I parsed it into jObject, I have another list which flattened the properties of a jobject which share part of the property elements names. Now I want to modify values in the jobject according to values indicated in the list.
At first, I was recursively traversing and seeking tokes, and then edit the value, but it is very annoying and reads poorly, though it does achieve the same effect.
Then I realized there is jPath, which can find the dest token with expression. I then changed to using it. And finally there are not that many loops and recursions.
Some comparisons listed:
//the jobject
var a={a:{b:{c:3}}}
//the list
{"a:b:c" :3}
Using loop
//var newValObj = new JObject();
//var ret=new JObject();
//for (int i = 0; i < keyIndexerArray.Length; i++)
//{
// var key = keyIndexerArray[i];
// newVal[key] = new JObject();
// if(0==i)ret=newVal;
// if (i == keyIndexerArray.Length - 1) newVal[key] = newSettingVal;
// newVal = newVal[key] as JObject;
//}
}
//originalContent.Merge(newVal);
Using jpath
var newSettingVal = newItem.Value;
//JToken originalVal = originalContent;
var keyIndexerList = newItem.Key.Split(':').ToList();
var endingPath = keyIndexerList.Last();
keyIndexerList.RemoveAt(keyIndexerList.Count - 1);
var jPath = string.Join('.', keyIndexerList.ToArray());
var valueToken = originalContent.SelectToken(jPath);
valueToken[endingPath] = newSettingVal;
There is no merge, and every value is modified with this simple transformation. Loop the less the better, time complexity should have been reduced though I don't know how jPath is realized.
Some explanation and examples of using jPath by newtonsoft: Usage
And this short paragraph is not very expressive, but the key is to explain address changes in loops, note that, every reference is an address change, though it is not so evident in high level programming languages, but such code should always be explained in memory address maps.
And of course, it's to show off the English skill
Use JPath but not recursively loop a JObject to modify the values.的更多相关文章
- Bash For Loop Examples for Your Linux Shell Scripting--ref
There are two types of bash for loops available. One using the “in” keyword with list of values, ano ...
- forall 与 for loop 案例
create table a_tab(ver number,id number);create table b_tab(ver number,id number);set timing on DECL ...
- (转) Unreal的HLSL交叉编译-UEAPI
HLSL Cross Compiler This library compiles High Level Shading Language (HLSL) shader source code into ...
- Exercises for IN1900
Exercises for IN1900October 14, 2019PrefaceThis document contains a number of programming exercises ...
- leetcode 学习心得 (1) (24~300)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 24.Swap Nodes in Pairs Given a linked list, swap ever ...
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- C#解析json文件的方法
C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...
- JSON学习
1.JSON 语法是 JavaScript 对象表示语法的子集. l 数据在名称/值对中 l 数据由逗号分隔 l 花括号保存对象 l 方括号保存数组 JSON 值可以是: l 数字(整数或浮 ...
- 相克军_Oracle体系_随堂笔记004-shared pool
本章主要阐述SGA中的shared pool. Shared pool { 1.free 2.library cache(缓存sql语句及其执行计划) 3.row cache(数据字典缓存) } ...
随机推荐
- java的reflection
Reflection是Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说"自审",并能直接操作程序的内部属性.例如,使用它能获得 Java 类中 ...
- ci事务
CI框架百问百答:CodeIgniter的事务用法?--第9问 时间 2013-06-06 10:57:45 CSDN博客 原文 http://blog.csdn.net/haor2756/art ...
- FreeMarker(XML模板)导出word
在项目中使用它完成的功能是按照固定的模板将数据导出到Word.比如台账.在完成后将处理过程按照台账的要求导出,有时程序中需要实现生成标准Word文档,要求能够打印,并且保持页面样式不变. 这个功能就是 ...
- Win server 2016 升级 Win server 2019 [测试验证]
. 给win server 2016 挂在 win server 2019 的安装盘 2. 点击setup 直接进行安装操作 选择不下载更新, 然后到达输入序列号的界面 序列号为: WMDGN-G9 ...
- laravel 字段映射问题,表单中提交字段与数据表中字段不一致
在遇到提交表单时,表单中的name属性与数据表中的字段不一致,报错, 解决方法: 参考1:提交表单的时候,表单的name属性和数据表字段名称是一样的,这样有什么不妥么? 你数据库的信息给前端透露得越多 ...
- Java中TimeZone类的常用方法
一.TimeZone类的定义 TimeZone类是一个抽象类,主要包含了对于时区的各种操作,可以进行计算时间偏移量或夏令时等操作 二.TimeZone类的常用方法 1.getAvailableIDs( ...
- Shell Script的默认变量
$? #上一个命令执行后所回传的值,当我们执行某些命令时,这些命令都会回传一个执行后的代码.一般来说,如果成功执行该命令则会回传一个0值.如果执行过程发生错误,就会回传“错误代码” $$ #代表目前这 ...
- 第89天:HTML5中 访问历史、全屏和网页存储API
一.访问历史 API 通过history对象实现前进.后退和刷新之类的操作 history新增的两个方法history.replaceState()和history.pushState()方法属于HT ...
- 【ASP.NET Core 】ASP.NET Core 源码学习之 Logging[1]:Introduction
在ASP.NET 4.X中,我们通常使用 log4net, NLog 等来记录日志,但是当我们引用的一些第三方类库使用不同的日志框架时,就比较混乱了.而在 ASP.Net Core 中内置了日志系统, ...
- HDU2993_MAX Average Problem
题目要求你在n个数的序列中,找出一段连续的长度不小于k的连续的序列,使得这个序列的平均数最大.输出这个平均数. 典型的优先队列.首先我们需要根据输入的序列,制造一个和序列. 然后从k开始往后面走,其实 ...