我有一个'简单'的方案:阅读一些JSON文件,过滤或更改一些值并将结果的json写回,而不更改原始格式。C#操纵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
]
}
}
]
}
我已经试过JSON.Net通过newtonsoft等等,但只有这个我能找到的是:
- 读入对象
- 将对象写入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的简单,但我喜欢L.B.的更正式的方法。好一些。我个人不喜欢使用字符串值作为功能代码,但这只是我。如果我能接受这两个答案:我会的。我猜Shahin必须以'只'赞成一票。
的故事,我还没有真正通过这个工作,但它似乎是动态类型将是这个有用的。在这里看到:http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – McGarnagle