我有一个平坦的文件系统(字段固定的长度定义 - 我无法更改此遗留系统),其中序列顺序很重要,很重要。我正在尝试根据局限内的序列值获得一项求解。我有以下JSON对象:
{
APPOINTMENT_ID: {
value: '',
seqPos: 1,
dbSize: 2,
dbType: 'C',
xpath: 'DataSet0'
},
NAME: {
value: '',
seqPos: 2,
dbSize: 30,
dbType: 'C',
xpath: 'DataSet1'
},
DATE: {
value: '',
seqPos: 3,
dbSize: 10,
dbType: 'M',
xpath: 'DataSet2'
}
}
目前,我正在循环浏览索引和JSON进行比赛,然后设置局限。
JObject joOuter = null;
for (int i = 1; i <= joNode.Count; i++)
{
foreach (var jT in joNode)
{
if (i == jT.Value["seqPos"].Value<int>())
{
joOuter = jT.Value.Value<JObject>();
break;
}
}
// Do Work on the JObect
joOuter["value"] = "doProcess";
}
我想以1个循环而不是当前的嵌套代码进行此操作,而无需更改整个JSON并将键重新处理为序列位置。
我会将这些jobights复制到字典...
var jObj = JObject.Parse(jsonstr);
var dict = jObj.Properties().Select(x => x.Value)
.ToDictionary(x => (int)x["seqPos"], x => x);
//loop or access the objects by their index
dict[2]["value"] = "somevalue";
//see, jObj is changed accordingly....
var debug = jObj.ToString(Newtonsoft.Json.Formatting.Indented);