How would I Deserialize this JSON?

Oct 21, 2010 at 12:52 AM

I am new to JSON and the JSON.NET library, so I hope this question isn't too stupid. I amt trying to handle this JSON:

 

{"Stations":[{"Code":"A15","Lat":39.1199273249,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.1646273343,"Name":"Shady Grove","StationTogether1":"","StationTogether2":""},{"Code":"A14","Lat":39.0843216075,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.1461253392,"Name":"Rockville","StationTogether1":"","StationTogether2":""},{"Code":"A13","Lat":39.0624676517,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.1208179517,"Name":"Twinbrook","StationTogether1":"","StationTogether2":""},{"Code":"A12","Lat":39.0481513573,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.112829859,"Name":"White Flint","StationTogether1":"","StationTogether2":""},{"Code":"A11","Lat":39.02926895,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.10384972,"Name":"Grosvenor","StationTogether1":"","StationTogether2":""},{"Code":"A10","Lat":39.0000564843,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0969522905,"Name":"Medical Center","StationTogether1":"","StationTogether2":""},{"Code":"A09","Lat":38.9843936603,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0941291922,"Name":"Bethesda","StationTogether1":"","StationTogether2":""},{"Code":"A08","Lat":38.9594838736,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.084995805,"Name":"Friendship Heights","StationTogether1":"","StationTogether2":""},{"Code":"A07","Lat":38.9488514351,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0795873255,"Name":"Tenleytown","StationTogether1":"","StationTogether2":""},{"Code":"A06","Lat":38.9432652883,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0629861805,"Name":"Van Ness UDC","StationTogether1":"","StationTogether2":""},{"Code":"A05","Lat":38.9347628908,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0580425191,"Name":"Cleveland Park","StationTogether1":"","StationTogether2":""},{"Code":"A04","Lat":38.9250851371,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0524180207,"Name":"Woodley Park Zoo","StationTogether1":"","StationTogether2":""},{"Code":"A03","Lat":38.9095980575,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.0434143597,"Name":"Dupont Circle","StationTogether1":"","StationTogether2":""}]}

The only way I have been able to get it to work properly, is to strip out {"Stations": and the final }

I assume I'm just doing something wrong (and probably stupid).

When it works, I am using this object:

    [JsonObject]
    public class Station {
        // {"Code":"A15","Lat":39.1199273249,"LineCode1":"RD","LineCode2":null,"LineCode3":null,"LineCode4":null,"Lon":-77.1646273343,"Name":"Shady Grove","StationTogether1":"","StationTogether2":""}

        public string Code { get; set; }

        [JsonProperty(PropertyName = "Lat")]
        public float Latitude { get; set; }

        [JsonProperty(PropertyName = "Lon")]
        public float Longitude { get; set; }

        public string Name { get; set; }


    }

And I am consuming it as such:

JsonConvert.DeserializeObject<List<Station>>(json, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
My understanding is that the identifier I am removing basically just means that there is a single object "Stations" that contains an array of unspecified name? But I can't seem to fix it by creating a Stations object in anyway. Thank you in advance for any help.