如何转换JSONArray列出与GSON? [英] How to convert JSONArray to List with Gson?
问题描述
在我的Android项目我想收到的JSONArray转换到一个列表。随着这太回答我有一个远一点。我现在有以下的code:
In my Android project I'm trying to convert a received JSONArray to a List. With the help of this SO answer I got a bit further. I now have the following code:
Gson gson = new Gson();
JSONArray jsonArray = NetworkUtilities.getData("mymodeldata");
Type listType = new TypeToken<List<MyModel>>(){}.getType();
List<MyModel> myModelList = gson.fromJson(jsonArray, listType);
不幸的是,在最后一行,在GSON类型的方法fromJson(字符串型)不适用的参数(JSONArray,类型)投诉。我真的不知道该如何解决这个问题。
Unfortunately it complaints at the last line that the method fromJson(String, Type) in the type Gson is not applicable for the arguments (JSONArray, Type). I don't really know how to solve this.
有谁知道我可以解决这个问题?
Does anybody know how I can solve this?
推荐答案
如果您看到了答案,你可以看到,在 fromJson()
方法的第一个参数是字符串
(JSON对象)。尝试使用的toString()
在JsonArray是这样的: -
If you see the answer there, you can notice that the first parameter in the fromJson()
method was a String
(the json object). Try to use the toString()
on the JsonArray like this:-
List<MyModel> myModelList = gson.fromJson(jsonArray.toString(), listType);
这篇关于如何转换JSONArray列出与GSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!