Sunday, 15 June 2014

serialization - how to use gson handle dynamic response JSON data? using Gson and deserialize -



serialization - how to use gson handle dynamic response JSON data? using Gson and deserialize -

i have problem. navigation info sent json info dynamic.

{ "type": "featurecollection", "features": [ { "type": "feature", "geometry": { "type": "point", "coordinates": [ 127.032737, 37.260704 ] } }

coordinates in geometric this.

{ "type": "feature", "geometry": { "type": "linestring", "coordinates": [ [ 127.032680, 37.260549 ], [ 127.032680, 37.260549 ] ] } }

when type string in geometry "linestring", coordinates in geometry changes.

but geometry class this.

public class geometry { string type = ""; public list<string> coordinates; }

so want know how these dynamaic info gson ( java class )

in think, have utilize deserialize, don't know..

you can seek jsondeserializer deserializer per json construction determined @ run-time.

for more info have @ gson deserialiser example

sample code: (parsing logic geometry, modify per need)

class geometry { private string type = ""; private list<string> coordinates; // getter & setter } class geometrydeserializer implements jsondeserializer<geometry> { @override public geometry deserialize(final jsonelement json, final type typeoft, final jsondeserializationcontext context) throws jsonparseexception { geometry geometry = new geometry(); jsonobject jsonobject = json.getasjsonobject(); jsonelement features = jsonobject.get("features"); string type = ""; list<string> list = new arraylist<string>(); if (features != null) { type = features.getasjsonarray().get(0).getasjsonobject().get("geometry") .getasjsonobject().get("type").getasstring(); } else { type = jsonobject.get("geometry").getasjsonobject().get("type").getasstring(); } if ("linestring".equals(type)) { jsonarray coordinates = jsonobject.get("geometry").getasjsonobject() .get("coordinates").getasjsonarray(); (int = 0; < coordinates.size(); i++) { list.add(coordinates.get(i).getasjsonarray().get(i).getasstring()); } } else { jsonarray coordinates = features.getasjsonarray().get(0).getasjsonobject() .get("geometry").getasjsonobject().get("coordinates").getasjsonarray(); (int = 0; < coordinates.size(); i++) { list.add(coordinates.get(i).getasstring()); } } geometry.setcoordinates(list); geometry.settype(type); homecoming geometry; } } geometry info = new gsonbuilder() .registertypeadapter(geometry.class, new geometrydeserializer()).create() .fromjson(jsonstring, geometry.class); system.out.println(new gsonbuilder().setprettyprinting().create().tojson(data));

output 1:

{ "type": "point", "coordinates": [ "127.032737", "37.260704" ] }

output 2:

{ "type": "linestring", "coordinates": [ "127.03268", "37.260549" ] }

for more clarity, please have @ posts well:

how convert json string java object when construction different

gson parse items comma-separated-value string within json value

json serialization gson

No comments:

Post a Comment