Select to view content in your preferred language

Convert a graphicsLayer to JSON

12117
20
02-03-2012 03:25 PM
SowjanyaSunkara
Emerging Contributor
Is there a way to convert an entire GraphicsLayer to JSON?
I was able to use dojo.toJson(graphics.toJson()) and convert individual graphics from a GraphicsLayer to JSON, so could anyone throw light on how to convert the entire layer to JSON?
I'm trying to store/save the JSON format of a graphics layer and then re-load it back to the user.

Thnx in adavance,
SS.
0 Kudos
20 Replies
ashrafsalim
Emerging Contributor

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadJosn.aspx.cs" Inherits="ReadJosn" %>

<!doctype html>

<html>

<head>

    <title>...</title>

    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.2/js/dojo/dijit/themes/claro/claro.css">

    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.2"></script>

    <script type="text/javascript" language="Javascript">

        dojo.require("esri.map");

        dojo.require("esri.layers.FeatureLayer");

        dojo.require("esri.layers.graphics");

     

        var map;

        function init() {

            //var startExtent = new esri.geometry.Extent({ "xmin": -12505511, "ymin": 2230016, "xmax": -3699965, "ymax": 8100380, "spatialReference": { "wkid": 102100 } });

            map = new esri.Map("mapDiv");

            //create and add new layer

            var layer = new esri.layers.ArcGISTiledMapServiceLayer("");

            var sketchLayer = "";

            map.addLayer(layer);

         

            var jsonFS =

                    { "geometry": { "rings": [[[663791.8729519134, 2760558.7151297885], [663791.8729519134, 2760492.5691641634], [667099.1712331766, 2749776.9227328706], [656118.9409393826, 2749247.7550078686], [652547.0587956184, 2755994.6435016454], [663791.8729519134, 2760558.7151297885]]], "spatialReference": { "wkid": 32638, "latestWkid": 32638 } }, "symbol": { "color": [0, 0, 0, 64], "outline": { "color": [0, 0, 0, 255], "width": 1, "type": "esriSLS", "style": "esriSLSSolid" }, "type": "esriSFS", "style": "esriSFSSolid" } }

            ;

           // var temp = eval("(" + jsonFS + ")");

            //var sk = new esri.Graphic(eval("(" + jsonFS + ")"));

            alert("0");

            map.getLayer("sketchLayer").add(new esri.Graphic(eval("(" + jsonFS + ")")));

          

            map.addLayer(new esri.Graphic(eval("(" + jsonFS + ")")));

            alert("addLayer" + sk);

            //console.log(jsonFS.features);

            //var fs = new esri.tasks.FeatureSet(jsonFS);

            //var featureCollection = {

            //    layerDefinition: {

            //        "geometryType": "esriGeometryPoint",

            //        "fields": [

            //        {

            //        }

            //        ]

            //    },

            //    featureSet: fs

            //};

            //var jsonfl = new esri.layers.FeatureLayer(featureCollection, {

            //    mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,

            //    'id': 'jsonfl'

            //});

            //map.addLayer(jsonfl);

        }

        dojo.ready(init);

    </script>

</head>

<body class="claro">

    <div id="mapDiv" style="width: 900px; height: 600px; border: 1px solid #000;"></div>

</body>

</html>

no Graphic appear to map any idea ??? Help

0 Kudos