.NET|--杂类|--json文件未释放, 就开始反序列化, 报错Newtonsoft.Json Unexpected character encountered while parsing value: .Path

前言


一个看起来很莫名其妙的错误, json文件我打开看了下, 格式也都正确,

但是在vs中调试的时候, 监视 -- 查看 -- JSON可视化工具
查看json字符串的话, 
会提示"字符串未设置为JSON格式",

"监视 -- 查看 -- 文本可视化工具", 发现json格式确实看不出来任何问题.

报错

# 报错的代码( 就是这行代码报错的 ...)
var geoJsonObject = JObject.Parse(geoJson);
# 详细错误
Newtonsoft.Json.JsonReaderException:“Unexpected end of content while loading JObject. Path 'features[1962]', line 1967, position 7249.”

解决方案


我这个json文件是通过GDAL生成出来的, 而我生成出来后, 并没有把相关资源关闭, 
猜测可能会导致缓冲区中的数据未正确刷新到磁盘,这可能使得文件的部分内容丢失或不完整?

错误代码↓

# 错误代码

/// <summary>
/// 将Shp文件转换为GeoJson格式文件
/// </summary>
/// <param name="shpFilePath"></param>
/// <param name="geoJSONFilePath"></param>
/// <returns></returns>
public static bool Convert2GeoJSONFile(string shpFilePath, string geoJSONFilePath)
{
    bool isOk = false;

    GdalConfiguration.ConfigureGdal();
    GdalConfiguration.ConfigureOgr();

    if (string.IsNullOrWhiteSpace(shpFilePath) || string.IsNullOrWhiteSpace(geoJSONFilePath))
    {
        throw new ArgumentException("输入参数路径不合法");
    }

    OSGeo.GDAL.Gdal.AllRegister();
    OSGeo.OGR.Ogr.RegisterAll();

    // 为了支持中文路径,请添加下面这句代码
    OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    // 为了使属性表字段支持中文,请添加下面这句
    OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");

    Gdal.SetConfigOption("DXF_ENCODING", "UTF-8");

    //打开数据
    DataSource ds = Ogr.Open(shpFilePath, 0);
    if (ds == null)
    {
        throw new Exception($"打开文件失败{shpFilePath}");
    }
    using OSGeo.OGR.Driver dv = Ogr.GetDriverByName("GeoJSON");
    if (dv == null)
    {
        throw new Exception($"打开驱动失败:GeoJSON");
    }

       
    // 注意这行代码****************************就是这行代码的锅*********************************************************************************↓↓↓↓↓↓↓↓↓↓↓
    var ret = dv.CopyDataSource(ds, geoJSONFilePath, null);

    isOk = true;
    return isOk;
}

正确代码↓

/// <summary>
/// 将Shp文件转换为GeoJson格式文件
/// </summary>
/// <param name="shpFilePath"></param>
/// <param name="geoJSONFilePath"></param>
/// <returns></returns>
public static bool Convert2GeoJSONFile(string shpFilePath, string geoJSONFilePath)
{
    bool isOk = false;

    GdalConfiguration.ConfigureGdal();
    GdalConfiguration.ConfigureOgr();

    if (string.IsNullOrWhiteSpace(shpFilePath) || string.IsNullOrWhiteSpace(geoJSONFilePath))
    {
        throw new ArgumentException("输入参数路径不合法");
    }

    OSGeo.GDAL.Gdal.AllRegister();
    OSGeo.OGR.Ogr.RegisterAll();

    // 为了支持中文路径,请添加下面这句代码
    OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    // 为了使属性表字段支持中文,请添加下面这句
    OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");

    Gdal.SetConfigOption("DXF_ENCODING", "UTF-8");

    //打开数据
    DataSource ds = Ogr.Open(shpFilePath, 0);
    if (ds == null)
    {
        throw new Exception($"打开文件失败{shpFilePath}");
    }
    using OSGeo.OGR.Driver dv = Ogr.GetDriverByName("GeoJSON");
    if (dv == null)
    {
        throw new Exception($"打开驱动失败:GeoJSON");
    }
     // *********************************加上using即可*********************************↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    // 一定要加using, 否则生成的geoJSONFilePath一直被占用, 并且反序列化JSON的时候会报错 : Newtonsoft.Json Unexpected character encountered while parsing value: .Path...
    using var ret = dv.CopyDataSource(ds, geoJSONFilePath, null);

    isOk = true;
    return isOk;
}

摘抄文档

posted @ 2024-07-18 18:11  zh89233  阅读(20)  评论(0编辑  收藏  举报