Convert graphiclayer to JSON

1912
1
10-03-2013 12:11 AM
papegowda1
New Contributor
Is there a way to convert GraphicsLayer to JSON
when user draws a relining(lines, points and polygon) and i need to give option to save this redling data in a SQLLite database, so that user can view the saved redling in feature.
to make this i need to convert the graphic layer to json.

Thanks in advance
PGowda
0 Kudos
1 Reply
JasonKnisley
Occasional Contributor
If this answer helps you, please mark it as an answer. 😉

There are a couple of ways that you can go about this depending on your needs and if you are interested is saving all of the graphics in a layer at once, or if you need to save them one at a time...

1) Use a FeatureSet. Add all of graphics to a FeatureSet and then use FeatureSet.toJson() to serialize it and FeatureSet.fromJson() to deserialize it.

2) Serialize the main components of an individual graphic separately (if all you care about is geometry and you are programmatically defining a symbol, you don't really need to worry about saving the symbol or attributes).
[INDENT]a) Geometry: use GeometryEngine.geometryToJson() / GeometryEngine.jsonToGeometry()
b) Symbol: use Symbol.toJson() / SymbolHelper.createSymbol()
c) Attributes: use new JSONObject(graphic.getAttributes()).toString() / new JSONObject(jsonAttributes)[/INDENT]


In the case of the attributes for option #2, when you deserialize them you'll have to loop through jsonObject.keys() and build a Map<String, Object> of attributes to use when recreating the graphic.
0 Kudos