Definitely the easiest way to do that is using Gson's default parsing function fromJson()
.
There is an implementation of this function suitable for when you need to deserialize into any ParameterizedType
(e.g., any List
), which is fromJson(JsonElement json, Type typeOfT)
.
在您的例子中,只需要获得List<String>
的Type
,然后将JSON数组解析为Type
,如下所示:
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
JsonElement yourJson = mapping.get("servers");
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> yourList = new Gson().fromJson(yourJson, listType);
在你的例子中,yourJson
是JsonElement
,但也可以是String
,任何Reader
或JsonReader
.
你可能想看看Gson API documentation.