Solved! Go to Solution.
using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.SOESupport; public class JSonBiz { private const string JSON_GEOMETRY = "geometry"; private const string JSON_SPATIAL_REFERENCE = "spatialReference"; private const string JSON_RINGS = "rings"; private const string JSON_PATHS = "paths"; private const string JSON_POINTS = "points"; private const string JSON_Z = "z"; public static IGeometry ToGeometry(string strJson) { IGeometry pGeometry = null; JsonObject pJsonObject = null; JsonObject pJsonObjectGeometria = null; JsonObject pJsonObjectSpatialReference = null; ISpatialReference pSpatialReference = null; try { //Parse pJsonObject = new JsonObject(strJson); //Get geometry if (pJsonObject.TryGetJsonObject(JSON_GEOMETRY, pJsonObjectGeometria)) { pGeometry = getGeometry(pJsonObjectGeometria); //Set spatial reference if ((pGeometry != null) && pJsonObject.TryGetJsonObject(JSON_SPATIAL_REFERENCE, pJsonObjectSpatialReference)) { pSpatialReference = Conversion.ToSpatialReference(pJsonObjectSpatialReference.ToJson()); if ((pSpatialReference != null)) { pGeometry.SpatialReference = pSpatialReference; } } } } catch (Exception ex) { pGeometry = null; //Maybe show your own error } return pGeometry; } private static IGeometry getGeometry(JsonObject jsonObjectGeometry) { IGeometry pGeometry = null; IPoint pPoint = null; IZAware pZAware = default(IZAware); double dblZ = 0; object[] objArray = null; //Test all if (jsonObjectGeometry.TryGetArray(JSON_RINGS, objArray)) { //Polygon pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPolygon); } else if (jsonObjectGeometry.TryGetArray(JSON_PATHS, objArray)) { //Polyline pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPolyline); } else if (jsonObjectGeometry.TryGetArray(JSON_POINTS, objArray)) { //Multipoint pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryMultipoint); } else { //Point try { pPoint = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPoint); //z value if (jsonObjectGeometry.Exists(JSON_Z) && jsonObjectGeometry.TryGetAsDouble(JSON_Z, dblZ)) { //Test value if (double.IsNaN(dblZ)) { dblZ = 0; } //Set value pZAware = pPoint; pZAware.ZAware = true; pPoint.Z = dblZ; } pGeometry = pPoint; } catch (Exception ex) { pGeometry = null; } //Envelope if (pGeometry == null) { try { pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryEnvelope); } catch (Exception ex2) { pGeometry = null; } } } //Simplify (Maybe you don't need it) if ((pGeometry != null)) { ITopologicalOperator pTopologicalOperator = pGeometry; pTopologicalOperator.Simplify(); } return pGeometry; } public static string ToJSon(ref IGeometry pGeometry) { string strJson = null; strJson = System.Text.Encoding.ASCII.GetString(ESRI.ArcGIS.SOESupport.Conversion.ToJson(pGeometry)); return strJson; } }
The original link is broken. Here is the updated link I think..