Select to view content in your preferred language

JSON to graphic

2809
2
05-31-2012 10:48 AM
DavidMaltby
New Contributor
I have a custom .net web service that returns ESRI's json format. I am having trouble getting the results to map. Here is a sample of the json:
{"geometryType":"esriGeometryPolyline",
"spatialReference":{"wkid":4326},
"fields":[
{"name":"OID","type":"esriFieldTypeInteger","alias":"OID"},
{"name":"OBJECTID","type":"esriFieldTypeBigInteger","alias":"OBJECTID"},
{"name":"Id","type":"esriFieldTypeUnknown","alias":"Id"},
{"name":"GNIS_Name","type":"esriFieldTypeString","alias":"GNIS_Name"},
{"name":"Hydro_ID","type":"esriFieldTypeUnknown","alias":"Hydro_ID"},
{"name":"Name","type":"esriFieldTypeString","alias":"Name"},
{"name":"Feature","type":"esriFieldTypeString","alias":"Feature"},
{"name":"F_Code","type":"esriFieldTypeBigInteger","alias":"F_Code"},
{"name":"State","type":"esriFieldTypeString","alias":"State"},
{"name":"State_FIPS","type":"esriFieldTypeString","alias":"State_FIPS"},
{"name":"Region","type":"esriFieldTypeBigInteger","alias":"Region"},
{"name":"NHD_RCode","type":"esriFieldTypeString","alias":"NHD_RCode"},
{"name":"NHD_ComID","type":"esriFieldTypeUnknown","alias":"NHD_ComID"},
{"name":"GNIS_ID","type":"esriFieldTypeString","alias":"GNIS_ID"},
{"name":"From_Node","type":"esriFieldTypeUnknown","alias":"From_Node"},
{"name":"To_Node","type":"esriFieldTypeUnknown","alias":"To_Node"},
{"name":"Length_mi","type":"esriFieldTypeUnknown","alias":"Length_mi"},
{"name":"Enabled","type":"esriFieldTypeBigInteger","alias":"Enabled"},
{"name":"GM_f_code","type":"esriFieldTypeString","alias":"GM_f_code"},
{"name":"GM_hyc","type":"esriFieldTypeBigInteger","alias":"GM_hyc"},
{"name":"GM_lit","type":"esriFieldTypeBigInteger","alias":"GM_lit"},
{"name":"GM_nam","type":"esriFieldTypeString","alias":"GM_nam"},
{"name":"GM_soc","type":"esriFieldTypeString","alias":"GM_soc"},
{"name":"GM_exs","type":"esriFieldTypeBigInteger","alias":"GM_exs"},
{"name":"Shape_Leng","type":"esriFieldTypeUnknown","alias":"Shape_Leng"}],
"features":[
{"attributes":{"OID":1,"OBJECTID":2,"Id":0,"GNIS_Name":"Clarks River","Hydro_ID":6008640,"Name":"Clarks River","Feature":"Stream","F_Code":4,"State":"KY","State_FIPS":"21","Region":6,"NHD_RCode":"06040006000075","NHD_ComID":1863538,"GNIS_ID":"00489552","From_Node":0,"To_Node":1,"Length_mi":0.1423984,"Enabled":1,"GM_f_code":"BH140","GM_hyc":9,"GM_lit":9,"GM_nam":"Clarks River","GM_soc":"USA","GM_exs":1,"Shape_Leng":0.002271788},
"geometry":{"paths":[[[-88.2817643199999,36.7790401180001],[-88.2834317879999,36.7805830280001]]]}}]}

Ive tried this: featureSet = com.esri.ags.FeatureSet.fromJSON(event.toString());
but get this error JSONParseError: Unexpected R encountered
at com.esri.serialization.json::JSONTokenizer/parseError()

ANY help would be appreciated!!
Tags (2)
0 Kudos
2 Replies
ThomasMcCracken
New Contributor III
Hi, I'm not too familiar with bringing in JSON objects but I believe yuo want to use the decode method of the JSON class in the com.esri.ags.utils package here:  http://help.arcgis.com/en/webapi/flex/apiref/index.html.
Good luck!

Thomas McCracken
GIS Analyst
Georgia Power Company
0 Kudos
IvanBespalov
Regular Contributor
from reference
See also
Input JSON should match this REST response syntax


What is valid syntaxis?
Make query and get results in JSON format
so valid JSON string for method FeatureSet.fromJSON() looks like
{
  "displayFieldName" : "description",
  "fieldAliases" : {
    "objectid" : "Object ID",
    "type" : "Type",
    "description" : "Description"
  },
  "geometryType" : "esriGeometryPoint",
  "spatialReference" : {
    "wkid" : 4326
  },
  "fields" : [
    {
      "name" : "objectid",
      "type" : "esriFieldTypeOID",
      "alias" : "Object ID"
    },
    {
      "name" : "type",
      "type" : "esriFieldTypeInteger",
      "alias" : "Type"
    },
    {
      "name" : "description",
      "type" : "esriFieldTypeString",
      "alias" : "Description",
      "length" : 1073741822
    }
  ],
  "features" : [
    {
      "attributes" : {
        "objectid" : 2010,
        "type" : 3,
        "description" : "xzc"
      },
      "geometry" : {
        "x" : -117.53253785200002,
        "y" : 34.244472716000075
      }
    },
    {
      "attributes" : {
        "objectid" : 2011,
        "type" : 0,
        "description" : "adf"
      },
      "geometry" : {
        "x" : -100.62294522999991,
        "y" : -15.344261442999937
      }
    }
  ]
}


sample app works fine with this JSON string
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags">
 
 <s:layout>
  <s:VerticalLayout gap="10"
        paddingBottom="10"
        paddingLeft="10"
        paddingRight="10"
        paddingTop="10" />
 </s:layout>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.symbols.SimpleFillSymbol;
   import com.esri.ags.symbols.SimpleLineSymbol;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   import com.esri.ags.utils.JSON;
   import com.esri.ags.utils.WebMercatorUtil;
   
   import mx.charts.CategoryAxis;
   import mx.controls.Alert;
   
   private const sms:SimpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_TRIANGLE, 22, 0x00FF00);
   private var sls:SimpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xFF0000);
   private var sfs:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_DIAGONAL_CROSS, 0x0000FF);
   
   protected function onAddButtonClick(event:MouseEvent):void
   {
    var jsonString:String = txtInput.text;
    if (jsonString.length > 0)
    {
     try
     {
      if (rbtnFeatureSetType.selected)
      {
       var featureSet:FeatureSet = FeatureSet.fromJSON(txtInput.text);
       if (featureSet && featureSet.features)
       {
        addGraphicsToMap(featureSet.features);
       }
      }
      else
      {
       var jsonObj:* = com.esri.ags.utils.JSON.decode(jsonString);
       Alert.show("JSON string is valid", "Message");
       // TODO: complete code
      }
     }
     catch (error:Error)
     {
      trace(error.getStackTrace());
      Alert.show("The input string cannot be parsed", "Error");
     }
     
    }
    else
    {
     Alert.show("You must input valid json string in text area", "Error");
    }
   }
   
   private function addGraphicsToMap(graphics:Array):void
   {
    if (graphicsLayer)
    {
     var isModified:Boolean = false;
     for (var i:int; i < graphics.length; i++)
     {
      var graphic:Graphic = graphics;
      if (graphic && graphic.geometry)
      {
       var geometry:Geometry = graphic.geometry;
       
       switch (geometry.type)
       {
        case Geometry.MAPPOINT:
        {
         graphic.symbol = sms;
         break;
        }
        case Geometry.POLYLINE:
        {
         graphic.symbol = sls;
         break;
        }
        case Geometry.POLYGON:
        {
         graphic.symbol = sfs;
         break;
        }
       }
       
       trace("Added graphic id=" + graphicsLayer.add(graphic));
      
       isModified = true;
      }
     }
     if (isModified)
     {
      graphicsLayer.refresh();
     }
    }
   }
  ]]>
 </fx:Script>
 
 
 <fx:Script>
  <![CDATA[
   protected function onClearButtonClick(event:MouseEvent):void
   {
    if (graphicsLayer)
    {
     graphicsLayer.clear();
    }
   }
  ]]>
 </fx:Script>
 
 <fx:Declarations>
  
  <s:RadioButtonGroup id="rbtnGroup"/>
       
 </fx:Declarations>
 
 <s:Panel title="Map panel"
    width="100%"
    height="100%">
  
  <s:layout>
   <s:VerticalLayout gap="5"
         verticalAlign="middle"
         horizontalAlign="center" />
  </s:layout>
  
  <s:Button label="Clear graphics"
      click="onClearButtonClick(event)" />
  
  <esri:Map id="map">
   <esri:ArcGISTiledMapServiceLayer id="baseLayer"
            url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer" 
            alpha="0.7" />
   <esri:GraphicsLayer id="graphicsLayer" />
  </esri:Map>
  
 </s:Panel>
 
 <s:Panel title="Input panel"
    width="100%"
    height="100%">
  
  <s:layout>
   <s:VerticalLayout gap="5"
         verticalAlign="middle"
         horizontalAlign="center" 
         paddingBottom="5"/>
   
  </s:layout>
  
  <s:TextArea id="txtInput" 
     width="100%"
     height="100%" />
  
  <s:HGroup width="100%"
      horizontalAlign="center"
      gap="20">
   
   <s:RadioButton id="rbtnFeatureSetType" 
         label="is FeatureSet"
         selected="true"
         group="{rbtnGroup}"/>
   
   <s:RadioButton id="rbtnCustomType" 
         label="is not FeatureSet"
         group="{rbtnGroup}" />
   
   <s:Button label="Add features"
       click="onAddButtonClick(event)" />
   
  </s:HGroup>  
  
 </s:Panel>
 
</s:Application>


to work with own JSON string you must create own serializer

P.S. yes, your json is valid json string, but not valid feature set - I did not find why 😞
0 Kudos