ArcGIS Runtime SDK for WPF - Library Reference
ToJson Method
See Also  Example
ESRI.ArcGIS.Client.Geometry Namespace > Geometry Class : ToJson Method

Serializes the geometry to JSON.

Syntax

Visual Basic (Declaration) 
Public Function ToJson() As String
C# 
public string ToJson()

Remarks

The Geometry.ToJson() Method is a very fast operation as it processes exclusively on the Client; no round trip to ArcGIS Server is required.

The Geometry.ToJson() Method is useful to obtain a JSON string representation from a Geometry object. This that can then serve as the parameter argument for the Shared/static Geometry.FromJson() Method.

NOTE: You should only pass JSON from Geometry objects into the Shared/static Geometry.FromJson() Method; do not use other JSON types (for example: JSON from an ESRI.ArcGIS.Client.Symbols.Symbol, etc.).

There are several use cases for using the Geometry.FromJson() and Geometry.ToJson() Methods. For example you could write custom undo/redo functions for Graphics editing. Another example would be to create a custom Server Object Extension (SOE) for an ArcObject's application.

Return Value

A JSON string representation of the geometry.

Example

How to use:

Click the button to execute a code-behind function QueryTask against a FeatureLayer in ArcGIS Server. The function loops through the Graphics in the returned FeatureSet and obtains the Json from the Graphics Geometry. Then new Graphics will be constructed using the Geometry.FromJson method to display in the Map.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Drawing Graphics on a Map using Geometry.ToJson and Geometry.FromJson via a QueryTask.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
  
  <!-- Add user defined resources for the symbology of the Graphics being drawn in the Map Controls. -->
  <Grid.Resources>
    <esri:SimpleFillSymbol x:Key="BufferSymbol" Fill="#66BB0000" BorderBrush="#88CC0000" BorderThickness="2"  />
    <esri:SimpleRenderer x:Key="PolygonRenderer" Symbol="{StaticResource BufferSymbol}"/>
  </Grid.Resources>
  
  <!-- Add a Map Control with a background ArcGISTiledMapServiceLayer zoomed to North America. There is also
  a placeholder GraphicsLayer that will display Graphics derived from Json based upon a QueryTask that will
  be added when the user clicks the button. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="0,121,0,0" Name="Map1" VerticalAlignment="Top" 
        WrapAround="True" Height="279" Width="600" Extent="-128.75,16.11,-65.00,56.75">
    <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
    <esri:GraphicsLayer ID="PolygonLayer" Renderer="{StaticResource PolygonRenderer}"/>
  </esri:Map>
  
  <!--Add a button to perform the work of adding the JSON based Graphics. -->
  <Button Height="23" HorizontalAlignment="Left" Margin="0,92,0,0" Name="Button1" VerticalAlignment="Top" 
          Width="600" Click="Button1_Click" Content="Click the button to draw Graphics derived from Json on the Map"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="102" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="600" TextWrapping="Wrap"
             Text="Click the button to execute a code-behind function QueryTask against a FeatureLayer in ArcGIS Server. 
             The function loops through the Graphics in the returned FeatureSet and obtains the JSON from the 
             Graphics Geometry. Then new Graphics will be constructed using the Geometry.FromJson method 
             to display in the Map." />
  
</Grid>
C#Copy Code
private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Create a new QueryTask object that will retrive data from an ArcGIS Server FeatureLayer web service.
  ESRI.ArcGIS.Client.Tasks.QueryTask theQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask();
  theQueryTask.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/4";
  
  // Wire up the QueryTask.ExecutedCompleted Event handler. This code in this event handler will draw Graphics on the Map using the
  // Geometry.ToJson and Geometry.FromJson Methods. 
  theQueryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
  
  // Wire up the QueryTask.Failed Event handler. This will trap any errors that might occur.
  theQueryTask.Failed += QueryTask_Failed;
  
  // Define a Query for the QueryTask to limit the scope of the FeatureSet returned.
  ESRI.ArcGIS.Client.Tasks.Query theQuery = new ESRI.ArcGIS.Client.Tasks.Query();
  theQuery.Where = "1=1";
  theQuery.ReturnGeometry = true;
  theQuery.ReturnZ = true;
  theQuery.ReturnM = true;
  
  // Execute the Asynchronous QueryTask. This will raise the QueryTask.Executed Completed Event.
  theQueryTask.ExecuteAsync(theQuery);
}
  
private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs e)
{
  // Get the FeatureSet from the QueryTaskEventArgs.
  ESRI.ArcGIS.Client.Tasks.FeatureSet theFeatureSet = e.FeatureSet;
  
  // Loop through each returned Graphic in the FeatureSet. 
  foreach (ESRI.ArcGIS.Client.Graphic theGraphic in theFeatureSet)
  {
    // Get the Geometry of the individual Graphic.
    ESRI.ArcGIS.Client.Geometry.Geometry theGeometry = theGraphic.Geometry;
    
    // Test to ensure that we can use the Geometry.ToJson and Geometry.FromJson Methods. 
    if (theGeometry is ESRI.ArcGIS.Client.IJsonSerializable)
    {
      // Get the JSON string from the Geometry of the Graphic.
      string theJsonString = theGraphic.Geometry.ToJson();
      
      // Create a new Geometry object. NOTE: The ESRI.ArcGIS.Client.Geometry.Geometry.FromJson() is a static Method.
      ESRI.ArcGIS.Client.Geometry.Geometry theGeometryFromJson = ESRI.ArcGIS.Client.Geometry.Geometry.FromJson(theJsonString);
      
      // Create a new Graphic with it's Geometry based upon a JSON string.
      ESRI.ArcGIS.Client.Graphic theGraphicFromJson = new ESRI.ArcGIS.Client.Graphic();
      theGraphicFromJson.Geometry = theGeometryFromJson;
      
      // Get the GraphicsLayer that was defined in XAML.
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer = Map1.Layers["PolygonLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      
      // Get the GraphicCollection from the GraphicsLayer.
      ESRI.ArcGIS.Client.GraphicCollection theGraphicsCollection = theGraphicsLayer.Graphics;
      
      // Add the Graphic created from JSON to the GraphicsLayer. The Map will automatically refresh with the new
      // Graphics when the loop is finished processing.
      theGraphicsCollection.Add(theGraphicFromJson);
    }
  }
}
  
private void QueryTask_Failed(object sender, ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs e)
{
  // If there is a problem with the QueryTask display the error information back to the user. 
  MessageBox.Show(e.Error.ToString());
}
VB.NETCopy Code
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Create a new QueryTask object that will retrive data from an ArcGIS Server FeatureLayer web service.
  Dim theQueryTask As ESRI.ArcGIS.Client.Tasks.QueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask
  theQueryTask.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/4"
  
  ' Wire up the QueryTask.ExecutedCompleted Event handler. This code in this event handler will draw Graphics on the Map using the
  ' Geometry.ToJson and Geometry.FromJson Methods. 
  AddHandler theQueryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
  
  ' Wire up the QueryTask.Failed Event handler. This will trap any errors that might occur.
  AddHandler theQueryTask.Failed, AddressOf QueryTask_Failed
  
  ' Define a Query for the QueryTask to limit the scope of the FeatureSet returned.
  Dim theQuery As ESRI.ArcGIS.Client.Tasks.Query = New ESRI.ArcGIS.Client.Tasks.Query
  theQuery.Where = "1=1"
  theQuery.ReturnGeometry = True
  theQuery.ReturnZ = True
  theQuery.ReturnM = True
  
  ' Execute the Asynchronous QueryTask. This will raise the QueryTask.Executed Completed Event.
  theQueryTask.ExecuteAsync(theQuery)
  
End Sub
  
Private Sub QueryTask_ExecuteCompleted(sender As Object, e As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
  
  ' Get the FeatureSet from the QueryTaskEventArgs.
  Dim theFeatureSet As ESRI.ArcGIS.Client.Tasks.FeatureSet = e.FeatureSet
  
  ' Loop through each returned Graphic in the FeatureSet. 
  For Each theGraphic As ESRI.ArcGIS.Client.Graphic In theFeatureSet
    
    ' Get the Geometry of the individual Graphic.
    Dim theGeometry As ESRI.ArcGIS.Client.Geometry.Geometry = theGraphic.Geometry
    
    ' Test to ensure that we can use the Geometry.ToJson and Geometry.FromJson Methods. 
    If TypeOf theGeometry Is ESRI.ArcGIS.Client.IJsonSerializable Then
      
      ' Get the JSON string from the Geometry of the Graphic.
      Dim theJsonString As String = theGraphic.Geometry.ToJson
      
      ' Create a new Geometry object. NOTE: The ESRI.ArcGIS.Client.Geometry.Geometry.FromJson() is a Shared Method.
      Dim theGeometryFromJson As ESRI.ArcGIS.Client.Geometry.Geometry = ESRI.ArcGIS.Client.Geometry.Geometry.FromJson(theJsonString)
      
      ' Create a new Graphic with it's Geometry based upon a JSON string.
      Dim theGraphicFromJson As ESRI.ArcGIS.Client.Graphic = New ESRI.ArcGIS.Client.Graphic
      theGraphicFromJson.Geometry = theGeometryFromJson
      
      ' Get the GraphicsLayer that was defined in XAML.
      Dim theGraphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = TryCast(Map1.Layers("PolygonLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      
      ' Get the GraphicCollection from the GraphicsLayer.
      Dim theGraphicsCollection As ESRI.ArcGIS.Client.GraphicCollection = theGraphicsLayer.Graphics
      
      ' Add the Graphic created from JSON to the GraphicsLayer. The Map will automatically refresh with the new
      ' Graphics when the loop is finished processing.
      theGraphicsCollection.Add(theGraphicFromJson)
      
    End If
  
  Next theGraphic
  
End Sub
            
Private Sub QueryTask_Failed(sender As Object, e As ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs)
  
  ' If there is a problem with the QueryTask display the error information back to the user. 
  MessageBox.Show(e.Error.ToString)
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8

See Also

© ESRI, Inc. All Rights Reserved.