Tags: json, oData |
Categories: Blog
Posted by
al on
7/30/2010 10:24 PM |
Comments (0)
Lately I have been using Json objects to consume data back and forward, is a great idea to use serialization and is the way that Json was defined to use, so let me provide you the class I have used to deserialize the Jsons strings to a C# class containing points and polygons.
Here many people would argue about using a oData provider, however oData does not have yet objects for point, lines and polygons, I hope that will be an extension soon for that.
JSON output of list of points:
{"displayFieldName":"user_","fieldAliases":{"objectid":"OBJECTID","user_":"User_","when_":"When_","message":"Message"},"geometryType":"esriGeometryPoint","spatialReference":{"wkid":4326},"fields":[{"name":"objectid","type":"esriFieldTypeOID","alias":"OBJECTID"},{"name":"user_","type":"esriFieldTypeString","alias":"User_","length":50},{"name":"when_","type":"esriFieldTypeDate","alias":"When_","length":36},{"name":"message","type":"esriFieldTypeString","alias":"Message","length":50}],"features":[{"attributes":{"objectid":556,"user_":null,"when_":null,"message":null},"geometry":{"x":-122.20946263799993,"y":46.347259295000072}},{"attributes":{"objectid":557,"user_":null,"when_":null,"message":null},"geometry":{"x":-121.7832936559999,"y":46.327096106000056}},{"attributes":{"objectid":558,"user_":null,"when_":null,"message":null},"geometry":{"x":-121.45076981699992,"y":46.223192199000039}},{"attributes":{"objectid":559,"user_":null,"when_":null,"message":null},"geometry":{"x":-121.60421939299994,"y":46.447141897000051}}]}
List of polygons
{"displayFieldName":"user_","fieldAliases":{"objectid":"OBJECTID","user_":"User_","when_":"When_","message":"Message","st_area(shape)":"st_area(shape)","st_length(shape)":"st_length(shape)"},"geometryType":"esriGeometryPolygon","spatialReference":{"wkid":4326},"fields":[{"name":"objectid","type":"esriFieldTypeOID","alias":"OBJECTID"},{"name":"user_","type":"esriFieldTypeString","alias":"User_","length":50},{"name":"when_","type":"esriFieldTypeDate","alias":"When_","length":36},{"name":"message","type":"esriFieldTypeString","alias":"Message","length":50},{"name":"st_area(shape)","type":"esriFieldTypeDouble","alias":"st_area(shape)"},{"name":"st_length(shape)","type":"esriFieldTypeDouble","alias":"st_length(shape)"}],"features":[{"attributes":{"objectid":851,"user_":null,"when_":1268488335000,"message":null,"st_area(shape)":0.034047438257996235,"st_length(shape)":0.77206915728427228},"geometry":{"rings":[[[-122.16579076999994,46.786080545000061],[-121.96758767299991,46.806778769000061],[-121.94071267599992,46.647888904000069],[-122.21282201299994,46.654806891000078],[-122.16579076999994,46.786080545000061]]]}}]}
JSON string to C# classes
public enum PositionType { none, esriGeometryPoint, esriGeometryPolygon }
public class Ref
{
public int wkid { get; set; }
}
public class SubObject
{
public Dictionary<string, string> attributes { get; set; }
}
public class SubObjectPoint : SubObject
{
public Position geometry { get; set; }
}
public class SubObjectPoly : SubObject
{
public Rings geometry { get; set; }
}
public class Rings
{
public ArrayList rings { get; set; }
}
public class Position
{
public double x { get; set; }
public double y { get; set; }
}
public class PointJson : Json
{
public PointJson() { features = new List<SubObjectPoint>(); }
public List<SubObjectPoint> features { get; set; }
}
public class PolygonJson : Json
{
public PolygonJson() { features = new List<SubObjectPoly>(); }
public List<SubObjectPoly> features { get; set; }
}
public class Json
{
public PositionType geometryType { get; set; }
public string displayFieldName { get; set; }
public Dictionary<string, string> fieldAliases { get; set; }
public Ref spatialReference { get; set; }
}
2099b5d8-ac22-4e50-8ce4-be06adb9185c|2|1.5