简体   繁体   English

IEnumerable的 <GeoJsonFeature<GeoJson2DGeographicCoordinates> &gt; .ToJson()由于对象ID产生无效的JSON?

[英]IEnumerable<GeoJsonFeature<GeoJson2DGeographicCoordinates>>.ToJson() produces invalid JSON due to Objectid?

I'm attempting to use the mongodb c# driver to generate json from a collection of GIS objects stored in the GeoJson format. 我正在尝试使用mongodb c#驱动程序从以GeoJson格式存储的GIS对象集合中生成json。 I then send this data to my page and use GeoJson.js to map the data using the Google Maps api. 然后,我将该数据发送到我的页面,并使用GeoJson.js通过Google Maps api映射数据。 Here's a simplified, hacky version of my controller method: 这是我的控制器方法的简化版本:

public JavaScriptResult GetMapData( IEnumerable<OilGasLeaseModel> leases )
{
  var client = new MongoClient(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
  var server = client.GetServer();
  var db = server.GetDatabase("GIS");
  var collection = db.GetCollection<GeoJsonFeature<GeoJson2DGeographicCoordinates>>("Sections");

  HashSet<string> sections = new HashSet<string>();
  foreach (OilGasLeaseModel ogl in leases)
  {
    foreach (TractModel tract in ogl.Tracts)
    {
      sections.Add(String.Format("{0}-T{1}-R{2}", tract.sec.TrimStart('0'), tract.twn, tract.rng));
    }
  }

  //var query = Query<GeoJsonFeature<GeoJson2DGeographicCoordinates>>.Where(x => x.Properties["SECTION"].ToString() == "20-T15N-R05W" );
  var result = collection.FindAll().Where(x => sections.Contains( x.Properties["SECTION"].AsString  ) );
  return JavaScript(result.ToJson());
}

this code generates JSON that looks like: 此代码生成的JSON如下所示:

[{ "type" : "Feature", "geometry" : { "type" : "Polygon", "coordinates" : [[[-97.602787016351414, 35.885755787882609], [-97.602844545557346, 35.881887460152967], [-97.6029024469591, 35.877283343404464], [-97.602876585570769, 35.871308155111066], [-97.616576075082719, 35.8711729277396], [-97.620908123078735, 35.871311565900726], [-97.620823160030156, 35.878294258522239], [-97.620823169953638, 35.881567848717381], [-97.620766650314266, 35.88571083185078], [-97.613535371346188, 35.88577902182432], [-97.611143428162308, 35.885803641463667], [-97.602787016351414, 35.885755787882609]]] }, "properties" : { "Shape_area" : 2615635.67494, "SECT_NUM" : "10", "SECTION" : "10-T16N-R4W", "EAST_WEST" : "W", "NORTH_SOUT" : "N", "Order_" : 0, "RANGE" : "4", "Shape_len" : 6453.04509751, "STR" : "10-16N-04W", "OBJECTID_1" : 2071, "TOWNSHIP" : "16", "MERIDIAN" : "IM" }, "_id" : ObjectId("52ddb2399f3a3124806d5a65") }, ... ]

the problem is that this is apparently not valid JSON, as I get an the error: 问题是,这显然不是有效的JSON,因为出现错误:

Uncaught ReferenceError: Objectid is not defined

When I try to use the resulting JSON as javascript. 当我尝试使用生成的JSON作为JavaScript时。

Is there some way that I can get the ToJson extension method to just convert the _id to a string, instead of ObjectId("52ddb2399f3a3124806d5a65") ? 有什么方法可以使ToJson扩展方法将_id转换为字符串,而不是ObjectId(“ 52ddb2399f3a3124806d5a65”)吗?

Any tips would be appreciated! 任何提示将不胜感激!

Ok, turns out that the Mongodb c# driver makes this very easy: 好的,事实证明,Mongodb c#驱动程序使此操作非常容易:

replace: 更换:

return JavaScript(result.ToJson());

with: 有:

return JavaScript(result.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM