RedEagle RedEagle - 1 year ago 83
Java Question

Gson Array of Strings to JsonArray

I'm using Gson and am trying to add a bunch of string values into a

like this:

JsonArray jArray = new JsonArray();

The problem is that the add method only takes a

I've tried to cast a String into a
but that didn't work.

How do I do it using Gson?

Answer Source

You can create a primitive that will contains String value and add it to array:

JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive("value1");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download