【发布时间】:2012-06-15 09:09:48
【问题描述】:
我有一个“简单”的场景:读取一些 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 变量的值。我希望也可以更改几何形状 inside 的值。我需要参考,而不是副本!这可能吗?
更新
我目前已退出此项目,但我想向回答者提供反馈。虽然我喜欢 Shahin 的简单,但我更喜欢 L.B. 更正式的方法。好一点。我个人不喜欢使用字符串值作为功能代码,但这只是我。如果我能接受这两个答案:我愿意。我想 Shahin 将不得不“仅仅”投一个赞成票。
【问题讨论】:
-
我实际上并没有解决这个问题,但似乎动态类型对此很有用。见这里:stackoverflow.com/questions/3142495/…