Home > Software engineering >  Emptying the object after deserializing a geoJson file in C#
Emptying the object after deserializing a geoJson file in C#

Time:12-19

I have a geoJson file of which this is a part

{"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}},
....

I want to read this file in C# and edit it based on the property and the code of that property.

I used this view model for deserialization... `

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

` But here the root and deserialize.properties empty are returned?!!!!!

CodePudding user response:

The main problem you have is that the class to which you are trying to deserialize do not correspond with your Json, you can use tools like https://json2csharp.com/ to generate the correct one.

Once you have to correct structures, you should be able to change it without any issue.

CodePudding user response:

Root.properties is defined as a List - but in your json it's just a single object.

Changing it to:

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

    public Geometry geometry { get; set; }

    public Properties properties { get; set; }
}

solves the problem.

Here's a working example:

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

On a side note, I would recommend using System.Text.Json instead.

CodePudding user response:

Based on your comments on my other answer and the code in your question, I'm guessing that you're trying to filter the feature collection based on the "Code" property. Try this:

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; }
}
  • Related