Java List<String> to JsonArray

摘要:Java List to JsonArray

    public JsonArray getStringListJsonArray(List list)
    {
        JsonParser parser = new JsonParser();
        Type listType = new TypeToken(){}.getType();
        Gson gson = new Gson();
        String json = gson.toJson(list,listType);
        JsonElement element = parser.parse(json);
        return element.getAsJsonArray();
    }