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. centos 6 编译emacs-24.5

    yum install `yum deplist emacs | grep provider | awk -F: '{print $2}' | awk '{print $1}' | xargs` yu ...

  2. mac python install zlib not available

    用brew install 3.4.4(python)时报 zipimport.ZipImportError: can't decompress data; zlib not available 的错 ...

  3. 【week11】回顾

    一.回答五个问题 第一次阅读<构建之法>之后的五个问题: 1.关于敏捷,书中说了我理解的就是介绍了敏捷就是“没有既定的计划与文档,马上写代码,随时发牢骚”,但是开发也是需要有一定的流程的, ...

  4. Bare metal APIs with ASP.NET Core MVC(转)

    ASP.NET Core MVC now provides a true "one asp.net" framework that can be used for building ...

  5. Windows搭建Log4Net+FileBeat+ELK日志分析系统过程

    参考博客:http://udn.yyuap.com/thread-54591-1-1.html ; https://www.cnblogs.com/yanbinliu/p/6208626.html ; ...

  6. CF530D sum in the tree

    我是题面.原题地址 很简单的一道贪心题 首先,先想想怎么判断是否合法 题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法! 所有深度为偶数的点的s被删除了,也只有深度为 ...

  7. 【BZOJ3771】Triple(生成函数,多项式运算)

    [BZOJ3771]Triple(生成函数,多项式运算) 题面 有\(n\)个价值\(w\)不同的物品 可以任意选择\(1,2,3\)个组合在一起 输出能够组成的所有价值以及方案数. \(n,w< ...

  8. 【BZOJ1416/1498】【NOI2006】神奇的口袋(数论,概率)

    [BZOJ1416/1498][NOI2006]神奇的口袋(数论,概率) 题面 BZOJ1416 BZOJ1498 洛谷 题面都是图片形式是什么鬼.. 题解 考虑以下性质 1.\(x[1],x[2]. ...

  9. BZOJ3295:[CQOI2011]动态逆序对——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3295 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数 ...

  10. 《Java程序设计》第8周学习总结 20165218 2017-2018-1

    20165218 2017-2018-1 <Java程序设计>第8周学习总结 教材学习内容总结 第12章 java多线程机制 java中的线程 计算机在任何给定时刻只能执行一个线程,多线程 ...