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.的更多相关文章

  1. 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 ...

  2. forall 与 for loop 案例

    create table a_tab(ver number,id number);create table b_tab(ver number,id number);set timing on DECL ...

  3. (转) Unreal的HLSL交叉编译-UEAPI

    HLSL Cross Compiler This library compiles High Level Shading Language (HLSL) shader source code into ...

  4. Exercises for IN1900

    Exercises for IN1900October 14, 2019PrefaceThis document contains a number of programming exercises ...

  5. leetcode 学习心得 (1) (24~300)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 24.Swap Nodes in Pairs Given a linked list, swap ever ...

  6. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  7. C#解析json文件的方法

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  8. JSON学习

    1.JSON 语法是 JavaScript 对象表示语法的子集. l  数据在名称/值对中 l  数据由逗号分隔 l  花括号保存对象 l  方括号保存数组 JSON 值可以是: l  数字(整数或浮 ...

  9. 相克军_Oracle体系_随堂笔记004-shared pool

    本章主要阐述SGA中的shared pool. Shared pool { 1.free 2.library cache(缓存sql语句及其执行计划) 3.row cache(数据字典缓存) }   ...

随机推荐

  1. QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写   ...

  2. paoding rose controller包及文件名命名规则

    1.包命名规则:xxx.xxx.controllers(否则扫描不到)

  3. PictureBox使用异常

    PictureBox的使用 代码: 显示部分 当我切换不同位置之间的照片时,出现这种问题: 但是当我代码改成下面的代码时,则错误消除 但我并不清楚原因

  4. 【UNIX环境高级编程】线程同步

    当多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的,那么就不存在一致性问题.同样,如果变量是只读的也不会有一致性问题.但是,当一个线程可 ...

  5. 第132天:移动web端-rem布局(进阶)

    rem布局(进阶版) 该方案使用相当简单,把下面这段已压缩过的 原生JS(仅1kb,源码已在文章底部更新,2017/5/3) 放到 HTML 的 head 标签中即可(注:不要手动设置viewport ...

  6. 如何在VScode中添加代码片段

    拿 VUE 举例,新建 VUE 文件,输入前缀,出现代码段 文件 --->  首选项 ---> 用户代码片段  在输入框中输入 vue ,找到 vue.json ,然后在 vue.json ...

  7. HDU 4767——Bell

    昨天比赛被虐的这个题目. 今天听斌牛讲过他的思路后就A掉了. 题目的意思是要你求出bell数的第n项对95041567取模. 首先,95041567=31*37*41*43*47: 然后取模就是先分别 ...

  8. bzoj3622-已经没有什么好害怕的的了

    题意 给出两个长度为 \(n\) 的数列 \(a,b\) ,\(2n\) 个数都互不相同,求有多少种对应方式使得 \(a_i>b_i\) 的个数比 \(a_i<b_i\) 的个数恰好多 \ ...

  9. 后缀树的线性在线构建-Ukkonen算法

    Ukkonen算法是一个非常直观的算法,其思想精妙之处在于不断加字符的过程中,用字符串上的一段区间来表示一条边,并且自动扩展,在需要的时候把边分裂.使用这个算法的好处在于它非常好写,代码很短,并且它是 ...

  10. Codeforces Round #525 Div. 2 自闭记

    A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...