我有一個'簡單'的方案:閱讀一些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