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);

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?

推荐答案

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);

Json相关问答推荐

Vega-Lite(Deneb):难以将最小和最大值应用于折线图和文本标签以及线条末尾的点

VBA json按特定属性名称提取所有数据

如何在Swift中使用JSON编码器的泛型

使用JQ将JSON输出转换为CSV复杂 struct

使用不同行数的数据创建嵌套Jolt

添加到数组时出错:找不到Add的重载和参数计数:1

如何在 jQuery 中循环遍历 JSON 数组?

如何在不解析的情况下在javascript中同步包含JSON数据?

谷歌浏览器不允许我放置断点

Jsonpath 与 Jackson 或 Gson

如何在 json 编码字符串内的子数组数据周围添加方括号?

AJAX 将 JavaScript 字符串数组发布到 JsonResult 作为 List 总是返回 Null?

有什么方法可以判断您安装的 gulp 版本吗?

如何使用 Gson 解码具有未知字段的 JSON?

如何在 Python 中合并两个 json 字符串?

如何从 MVC 控制器返回 Json 对象以查看

如何从 MySQL 中检索 JSON 数据?

在 .NET 中缩小缩进的 JSON 字符串

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题

为什么 jqXHR.responseText 返回字符串而不是 JSON 对象?