Thursday, 15 September 2011

java - Creating an array with Gson -



java - Creating an array with Gson -

i using gson create json objects in java. trying create array 3 elements:

[ "one", "two", "three" ]

with code:

jsonarray array = new jsonarray (); array.add("one"); array.add("two"); array.add("three");

but add() accepts jsonelement object, rather actual string.

the reason i'm under impression should able this, because i've used c# script called simplejson unity3d in past. it, this:

jsonarray ary = new jsonarray (); ary.add("one"); ary.add("two"); ary.add("three");

which works fine. i'm not sure how gson.

i know can convert java array json object:

string[] strings = {"abc", "def", "ghi"}; gson.tojson(strings); ==> prints ["abc", "def", "ghi"]

however, want dynamically createobjects in jsonarray (the add method), can c#.

jsonprimitive. should able utilize array.add(new jsonprimitive(yourstring);

java json gson

No comments:

Post a Comment