我有一个平面文件系统(字段的固定长度定义 - 我无法更改此遗留系统),其中序列顺序对于写入平面文件很重要。我试图基于JObject内的序列值获得一个JObject。我有以下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。
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并将密钥重新调整为序列位置。
答案 0 :(得分:1)
我会将这些JObject复制到字典中......
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);