本文介绍了Gson数组字符串到JsonArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Gson,并试图将一堆字符串值添加到 JsonArray
中,如下所示:
JsonArray jArray = new JsonArray();
jArray.add(value1);
问题是add方法只需要 JsonElement
我试图将一个字符串转换为 JsonElement
,但这不起作用。
我如何使用Gson?
解决方案
创建一个包含String值的原语并将其添加到数组中:
JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive(value1);
jArray.add(element);
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?
解决方案
You can create a primitive that will contains String value and add it to array:
JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive("value1");
jArray.add(element);
这篇关于Gson数组字符串到JsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!