Select to view content in your preferred language

Convert a graphicsLayer to JSON

12114
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
derekswingley1
Deactivated User
Unfortunately, no, graphics layers do not have a toJson() method.

Can you switch to using a feature layer? Those do have a toJson() method which would do exactly what you want.
0 Kudos
SowjanyaSunkara
Emerging Contributor
Derek - Thanks for the prompt response. The reason for using a graphics layer is to save the user drawn graphics (Sketch/Pushpins), on the server and load it back when needed.
So do I have to convert individual graphics and save them on the server or is there an other way around?
0 Kudos
derekswingley1
Deactivated User
If you want to stick with a graphics layer, then you'll have to loop through the array of graphics and call dojo.toJson(graphic.toJson()) to get a text string you can push back to the server to be saved.
0 Kudos
SowjanyaSunkara
Emerging Contributor
Thanks Derek.
0 Kudos
SowjanyaSunkara
Emerging Contributor
So, I successfully saved all map graphics (sketches/pushpins) on the server but was having no luck re-drawing them back on the map.
  var sk = new esri.Graphic(sketches);
        map.getLayer("importSketchLayer").add(sk);

and a sample sk ={"geometry":{"rings":[[[2479931.879726085,7106033.410997431],[2480065.4169330294,7106050.981682555],[2480110.222180096,7105950.828777347],[2480014.461946169,7105920.958612636],[2479931.879726085,7106033.410997431]]],"spatialReference":{"wkid":2276}},"attributes":{"sketchType":"shape"},"symbol":{"color":[255,255,102,255],"outline":{"color":[192,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}}

Am I missing something?
0 Kudos
derekswingley1
Deactivated User
That looks like it should work. What's the spatial reference for your map? Are there any errors after you call add()?
0 Kudos
SowjanyaSunkara
Emerging Contributor
There are no errors.  I donot see the graphics on the map.
In debug, I noticed that geometry values for sk is returned as "null" after the assignment
var sk = new esri.Graphic(sketches);

where

sketches={"geometry":{"rings":[[[2479931.879726085,7106033.410997431],[2480065.4169330294,7106050.981682555],[2480110.222180096,7105950.828777347],[2480014.461946169,7105920.958612636],[2479931.879726085,7106033.410997431]]],"spatialReference":{"wkid":2276}},"attributes":{"sketchType":"shape"},"symbol":{"color":[255,255,102,255],"outline":{"color":[192,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}}

my map.SpatialReference wkid: 2276
0 Kudos
SowjanyaSunkara
Emerging Contributor
I fixed it. I had to do a eval on the json returned so geometry and symbol are not null.


var temp = eval("("+ sketches + ")");
var sk = new esri.Graphic(temp);
map.getLayer("sketchLayer").add(sk);
0 Kudos
derekswingley1
Deactivated User
How are you getting the JSON from the server? If you use dojo.io.script.get or dojo.xhrGet (with handleAs: "json") you wouldn't need to do the eval (it's done inside the dojo functions).
0 Kudos