Micah Armantrout Micah Armantrout - 10 days ago 9
C# Question

Latitude Longitude JSON C# Serialization

I would like to take this class and convert it to JSON.

public class Location
{
public string city {get; set;}
public double population {get; set;}
public double Latitude {get; set;}
public double Longitude {get; set;}
}


In this format by JSON.NET

[
{
"city ": "Atlanta, GA",
"Value": 520,
"Location": [
42.7,
23.33
]
}
]

Answer

It's a bit strange format for this class. So try:

public class Location
{
    [JsonProperty("City")]
    public string City {get; set;}
    [JsonProperty("Value")]
    public double Population {get; set;}
    [JsonIgnore]
    public double Latitude {get; set;}
    [JsonIgnore]
    public double Longitude {get; set;}
    public double[] Location 
    {
        get { return new double[] { Latitude, Longitude }; }
    }
}

Although the custom JsonConverter, may be a cleaner solution.