我有一个“简单”的场景:读取一些 JSON 文件,过滤或更改一些值,然后在不更改原始格式的情况下将生成的 json 写回。
所以例如改变这个:
{
"type": "FeatureCollection",
"crs": {
"type": "EPSG",
"properties": {
"code": 28992
}
},
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
149886.192,
374554.705
],
[
149728.583,
374473.112
],
[
149725.476,
374478.215
]
]
]
}
}
]
}
进入这个:
{
"type": "FeatureCollection",
"crs": {
"type": "EPSG",
"properties": {
"code": 28992
}
},
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates":
[
149886.192,
374554.705
]
}
}
]
}
我已经尝试过 newtonsoft 的 JSON.Net,但我唯一能找到的是:
- 读入对象
- 将对象写入json
但我错过了“更改对象”步骤。有什么提示吗?
更新
这是我到目前为止所尝试的:
JToken contourManifest = JObject.Parse(input);
JToken features = contourManifest.SelectToken("features");
for (int i = 0; i < features.Count(); i++)
{
JToken geometry = features[i].SelectToken("geometry");
JToken geoType = geometry.SelectToken("type");
JToken coordinates = geometry.SelectToken("coordinates");
geoType = "Point";
}
但这只会改变 geoType 变量的值。我也希望改变几何内部的值。我需要参考,而不是副本!这可能吗?
更新
我目前不在这个项目中,但我想向回答者提供我的反馈。虽然我喜欢 Shahin 的简单性,但我更喜欢 LB 更正式的方法。我个人不喜欢使用字符串值作为功能代码,但这只是我。如果我能接受这两个答案:我愿意。我猜沙欣将不得不“仅仅”投赞成票。