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(数据字典缓存) } ...
随机推荐
- ansible介绍和安装
ansible是由 Python 编写的强大的配置管理解决方案,ansible 的特点就在于它的简洁与高效率 ansible与其他的配置管理工具不同点在于:不需要你在想要配置的每个节点上安装自己的组件 ...
- cobbler-web 网络安装服务器套件 Cobbler(补鞋匠)
Cobbler作为一个预备工具,使部署RedHat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署. 它提供以下服务集成: * PXE服务支持 * DHCP服务管 ...
- PHP对象类型转换
其他数据类型转换为对象类型 其他数据类型转换为对象类型,得到的结果是:内置标准类(stdclass)的一个对象! 语法形式为: $obj1 = (object) 其他类型数据: 数组转换为对象:数 ...
- CPU测试--通过proc获取CPU信息
adb shell cat /proc/stat | grep cpu > totalcpu0 此处第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了.下表解析第一行 ...
- ZOJ3113_John
这个题目是一个典型的Anti_Sg.我也不知道为什么这么叫,呵呵,反正大家都这么叫,而且我也是听别人说,看别人的日志自己才知道的. 题目的意思是给你不同颜色的石子,每次可以去一种颜色的石子若干个(至少 ...
- java 常量 jvm在编译期能确定常量的 所以相等 但不能确定变量的范围 所以变量相加会产生新的
- bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...
- robot framework Selenium2关键字介绍
*** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...
- bzoj3251: 树上三角形(思维题)
神tmWA了8发调了20min才发现输出没回车T T... 首先考虑一段什么样的序列才会是N... 显然最长的形式就是斐波那契,前两数之和等于第三数之和,这样就无法组成三角形并且序列最长.可以发现在i ...
- 【agc004F】Namori
Portal -->agc004F Solution 好神仙的转化qwq 首先我们可以先考虑\(m=n-1\)的情况下,也就是树的情况下要怎么做 我们可以将这个问题转化一下:我们对这颗 ...