Gson Array of Strings to JsonArray

gson, java, json

Solution

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

JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive("value1");
jArray.add(element);

Problem

I'm using Gson and am trying to add a bunch of string values into a `JsonArray` like this: ``` JsonArray jArray = new JsonArray(); jArray.add("value1"); ``` The problem is that the add method only takes a `JsonElement`. I've tried to cast a String into a `JsonElement` but that didn't work. How do I do it using Gson?

Original source