如何处理包含许多对象和属性的 Json 数组响应?

How to address a Json array response with many objects and properties?

我需要帮助来了解解决 JSON 响应的推荐方法是什么 json 将显示的对象和属性(其中一些,其他可能会显示在 window 的详细信息中,当用户点击 listitem 时)在 Android 的 ListView 中。

到目前为止,我知道您可能会使用 JSONObject class 来获取属性:

JSONObject resultObject = resultsArray.getJSONObject(i);

String title = resultObject.getString("original_title");
String posterPath = resultObject.getString("poster_path");
String overview = resultObject.getString("overview");
String date = resultObject.getString("release_date");


// Create a new {@link Movie} object with the magnitude, location, time,
// and url from the JSON response.
Movie movie = new Movie(title, posterPath, overview, date);

// Add the new {@link Movie} to the list of movies.
movies.add(movie);

但我的主要疑问是,如果 JSON 属性更多,即需要添加到 ListView 的 20 个属性会怎样:

最好的选择是什么?考虑到 JSON 将包含对象,例如:电影、演员、统计数据等

我是否必须创建不同的 Java bean classes 才能创建实例?例如:

Movie movie = new Movie(title, posterPath, overview, date);

所以我想避免有一个巨大的构造函数参数。我应该只创建一个 class 并使用构建器模式吗?

您可以使用 GSON 库反序列化您的 JSON 数据。

您的 Movie class 会像:

class Movie {
    String title;
    String posterPath;
    String overview;
    String date;
}

并解析为:

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Movie movie = gson.fromJson(jsonInput, Movie.class);

然后你只需要更新你的 Movie class 如果你想要 add/update 个字段