Sean
Sean

Reputation: 1123

how to create a Json array of strings in android

I am trying to create an array like this ["stringone","stringtwo"] and pass it to the webserver. When I tried doing making it with a string array like String[]={"stringone","stringtwo"] it passed in something weird {"userids":"[Ljava.lang.String;@406fe4b8"} how should I be constructing my JSON array if not by using string arrays?

Thanks

Upvotes: 3

Views: 6953

Answers (2)

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

If you want to create JSONArray from List or array, you can use constructor, which takes Collection:

String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.asList(data));

Upvotes: 10

Ian G. Clifton
Ian G. Clifton

Reputation: 9439

The easiest way is to create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.

Upvotes: 3

Related Questions