簡體   English   中英

在 C# 中反序列化 geoJson 文件后清空對象

[英]Emptying the object after deserializing a geoJson file in C#

我有一個 geoJson 文件,這是其中的一部分

{"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[45.882982627281955,35.98144306876872],[45.8830448154499,35.98142063110326],[45.883106013386524,35.98143674855534],[45.883177395327635,35.981590195979166],[45.88306057502328,35.98161790966196],[45.882982627281955,35.98144306876872]]]},"properties":{"Code":1,"Landuse":"fde","Longitude":45.8830793043,"latitude":35.9815185013}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[45.88321822952168,35.98143433703011],[45.88329577844585,35.981578778123584],[45.883184747057655,35.98160599975271],[45.883177395327635,35.981590195979166],[45.88313701140243,35.981503383976175],[45.883107851319025,35.981440699498734],[45.88321822952168,35.98143433703011]]]},"properties":{"Code":2,"Landuse":"fde","Longitude":45.8832014571,"latitude":35.9815182472}},
....

我想在 C# 中讀取此文件並根據屬性和該屬性的代碼對其進行編輯。

我使用這個視圖模型進行反序列化......`

public class Geometry
        {
            public string type { get; set; }
            public List<List<List<double>>> coordinates { get; set; }
        }

        public class Properties
        {
            public int Code { get; set; }
            public string Landuse { get; set; }
            public double Longitude { get; set; }
            public double latitude { get; set; }
        }

        public class Root
        {
            public string type { get; set; }
            public Geometry geometry { get; set; }
            public List<Properties> properties { get; set; }
        }

I use these codes to deserialize and edit the geoJson file...

string json = File.ReadAllText(myPath);
                    var deserialize = JsonConvert.DeserializeObject<Root>(json);
                    foreach (var item in deserialize.properties)
                    {
                        if (item.Code == 2)
                        {
                            item.Landuse = ppp;
                        }
                    }
                    string output = Newtonsoft.Json.JsonConvert.SerializeObject(deserialize);
                    File.WriteAllText(myPath, output);
                }

` 但是這里返回的是 root 和 deserialize.properties 空的?!!!!!!

您遇到的主要問題是您嘗試反序列化的類與您的 Json 不對應,您可以使用https://json2csharp.com/等工具生成正確的類。

一旦你必須糾正結構,你應該能夠毫無問題地改變它。

Root.properties 被定義為一個列表 - 但在您的 json 中它只是一個對象。

將其更改為:

public class Root
{
    public string type { get; set; }

    public Geometry geometry { get; set; }

    public Properties properties { get; set; }
}

解決了問題。

這是一個工作示例:

using Newtonsoft.Json;

string json = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[45.882982627281955,35.98144306876872],[45.8830448154499,35.98142063110326],[45.883106013386524,35.98143674855534],[45.883177395327635,35.981590195979166],[45.88306057502328,35.98161790966196],[45.882982627281955,35.98144306876872]]]},\"properties\":{\"Code\":1,\"Landuse\":\"fde\",\"Longitude\":45.8830793043,\"latitude\":35.9815185013}}";

Root result = JsonConvert.DeserializeObject<Root>(json);

Console.WriteLine(JsonConvert.SerializeObject(result));

public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class Properties
{
    public int Code { get; set; }
    public string Landuse { get; set; }
    public double Longitude { get; set; }
    public double latitude { get; set; }
}

public class Root
{
    public string type { get; set; }

    public Geometry geometry { get; set; }

    public Properties properties { get; set; }
}

另外,我建議改用 System.Text.Json。

根據您對我的其他答案的評論和問題中的代碼,我猜您正在嘗試根據“代碼”屬性過濾功能集合。 試試這個:

using Newtonsoft.Json;

string json = "{\"type\":\"FeatureCollection\", \"features\": [\r\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[45.882982627281955,35.98144306876872],[45.8830448154499,35.98142063110326],[45.883106013386524,35.98143674855534],[45.883177395327635,35.981590195979166],[45.88306057502328,35.98161790966196],[45.882982627281955,35.98144306876872]]]},\"properties\":{\"Code\":1,\"Landuse\":\"fde\",\"Longitude\":45.8830793043,\"latitude\":35.9815185013}},\r\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[45.88321822952168,35.98143433703011],[45.88329577844585,35.981578778123584],[45.883184747057655,35.98160599975271],[45.883177395327635,35.981590195979166],[45.88313701140243,35.981503383976175],[45.883107851319025,35.981440699498734],[45.88321822952168,35.98143433703011]]]},\"properties\":{\"Code\":2,\"Landuse\":\"fde\",\"Longitude\":45.8832014571,\"latitude\":35.9815182472}} ]}";

FeatureCollection? collection = JsonConvert.DeserializeObject<FeatureCollection>(json);

if (collection != null)
{
    foreach (var feature in collection.features)
    {
        if (feature.properties.Code == 2)
        {
            Console.WriteLine(JsonConvert.SerializeObject(feature));
        }
    }
}



public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class Properties
{
    public int Code { get; set; }
    public string Landuse { get; set; }
    public double Longitude { get; set; }
    public double latitude { get; set; }
}

public class Feature
{
    public string type { get; set; }

    public Geometry geometry { get; set; }

    public Properties properties { get; set; }
}

public class FeatureCollection
{
    public string type { get; set; }

    public List<Feature> features { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM