C#操纵JSON数据 [英] C# Manipulating JSON data

查看:139
本文介绍了C#操纵JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的场景:看了一些JSON文件,过滤器或改变一些价值观和写入生成的JSON回不改变原来的格式



因此,例如,可以改变:

  {
类型:FeatureCollection,
CRS :{
类型:EPSG,
特性:{
密码:28992
}
},
的特点:
{
类型:功能,
几何体:{
类型:多边形,
坐标:[
[
[
149886.192,
374554.705
],
[
149728.583,
374473.112
],

149725.476,
374478.215
]
]
]
}
}
]
}

进入这样的:

  {
类型:FeatureCollection,
CRS:{
类型:EPSG,
特性:{
密码:28992
}
},
特色:
{
类型:功能,
几何:{
类型:点,
坐标:
[
149886.192,
374554.705
]
}
}
]
}

我试过JSON。科净等等newtonsoft但只有这个我能找到的是:




  • 读入对象

  • 写对象的JSON



但我错过了'改变对象的一步。 ?任何提示



更新



下面是我到目前为止已经试过:

  JToken contourManifest = JObject.Parse(输入); 

JToken功能= contourManifest.SelectToken(特征);

的for(int i = 0; I< features.Count();我++)
{
JToken几何=功能[I] .SelectToken(几何);
JToken geoType = geometry.SelectToken(类型);
JToken坐标= geometry.SelectToken(坐标);

geoType =点;
}



但是,这只是改变了geoType变量的值。我有望改变值的的几何体也是如此。我需要一个参考,不是副本!这可能吗?



更新



目前我过这个项目,但我倒是想给我的反馈给回答者。虽然我喜欢沙欣的简单,我喜欢L.B.的更正式的方法好一点。我个人不喜欢使用字符串值作为功能代码,但是这只是我。如果我能接受两个答案:我会的。我想沙欣西港岛线必须做出应有的与'只是'一个给予好评。


解决方案

 动态contourManifest = JObject.Parse(输入); 
的foreach(在contourManifest.features VAR功能)
{
feature.geometry.Replace(
JObject.FromObject(
新的{
型=点
坐标= feature.geometry.coordinates [0] [0]
}));
}

VAR newJson = contourManifest.ToString();


I have a 'simple' scenario: Read some JSON file, Filter or change some of the values and write the resulting json back without changing the original formatting.

So for example to change this:

{
  "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
            ]
          ]
        ]
      }
    }
  ]
}

Into this:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "EPSG",
    "properties": {
      "code": 28992
    }
  },
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": 
            [
              149886.192,
              374554.705
            ]
      }
    }
  ]
}

I've tried JSON.Net by newtonsoft among others but the only this I can find is:

  • read into object
  • write object to json

But I'm missing the 'change the object' step. Any hints?

Update

Here's what I've tried so far:

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";
}

But this only changes the value of the geoType variable. I'd expected to change the value inside the geometry as well. I need a reference, not a copy! Is this possible?

Update

I am currently off this project but I'd like to give my feedback to the answerers. Though I like the simplicity of Shahin, I like the more formal approach of L.B. a bit better. I personally don't like using string values as functional code, but that's just me. If I could accept both answers: I would. I guess Shahin wil have to make due with 'just' an upvote.

解决方案

dynamic contourManifest = JObject.Parse(input);
foreach (var feature in contourManifest.features)
{
    feature.geometry.Replace(
            JObject.FromObject(
                        new { 
                            type = "Point", 
                            coordinates = feature.geometry.coordinates[0][0] 
                        }));
}

var newJson = contourManifest.ToString();

这篇关于C#操纵JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆