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(数据字典缓存) } ...
随机推荐
- jQuery之层次选择器
层次选择器: 查找子元素, 后代元素, 兄弟元素的选择器1. ancestor descendant 在给定的祖先元素下匹配所有的后代元素2. parent>child 在给定的父元素下匹配所有 ...
- java 基础 --Collection(Set)
注意: 如果hashSet存储自定义对象,一定要重写hashCode()&&equals() 如果TreeSet存储自定义对象,让元素所属的类实现自然排序接口Comparable,并重 ...
- Privoxy
Privoxy 前沿: 这个玩意我以前都没听说过,今天在别人的帮助下试了试,只想说:谁还能阻挡我,我就是要USA....ps:在此感谢每一个帮助我的人 介绍: Privoxy是一款带过滤功能的代理服务 ...
- sublime text 插件集锦
Markdown & OmniMarkupPreviewer插件 插件说明 Markdown : markdown语法编辑 OmniMarkupPreviewer :实时在浏览器预览, mac ...
- App流量测试--使用安卓自身提供的TCP收发长度统计功能
在Linux系统有3个地方保存流量统计文件,对于Android系统同样也适用: (1)在/proc/net/dev下可以查看各个网络接口的收发流量 (等同adb shell cat /proc/pi ...
- liunx less 命令
1.命令格式: less [参数] 文件 2.命令功能: less 与 more 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会 ...
- 基于三个kinect的人体建模
单个kinect的人体重建,在Kinect SDK 1.8中,Kinect Fusion的效果已经很不错了.其缺点显而易见,一是扫描时间长,重建对象也需要长时间保持静态:二是需要人体或者kine ...
- 洛谷 P3802 小魔女帕琪 解题报告
P3802 小魔女帕琪 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组合,从 ...
- django 自己编写admin
继上次CRM项目之后 我们发现了django自带admin的强大之处以及灵活性,但是admin在企业中也一样很难做到完全的对接,因此编写自己的后台管理就显得至关重要. 本次自定义admin项目将接着上 ...
- iOS9 HTTP请求失败
iOS9把所有HTTP请求都改成了HTTPS请求,导致应用加载不出数据. 解决方法:在plist中添加以下新字段 App Transport Security Settings:Dictionary ...