android - 如何使用 Retrofit Api 解析 Json 对象列表
问题描述
{
"0": {
"id": "1",
"img": "ginterests/1.png",
"title": "Training & Educations",
"description": "Yoga | Fitness | Daily Trainings | Dance | Music +",
"follow": 1
},
"1": {
"id": "2",
"img": "ginterests/2.png",
"title": "Consultations",
"description": "Legal | Studies | Entrance Exam Preparations +",
"follow": 1
},
"2": {
"id": "3",
"img": "ginterests/3.png",
"title": "Public Events",
"description": "City Events | Trending | Current Events +",
"follow": 0
},
"3": {
"id": "4",
"img": "ginterests/4.png",
"title": "Business Events",
"description": "Talks & Shows | Press | Live coverage | Business meets +",
"follow": 0
},
"4": {
"id": "5",
"img": "ginterests/5.png",
"title": "Performances",
"description": "Regional & Global | Celebreties | Standup +",
"follow": 0
},
"5": {
"id": "6",
"img": "ginterests/6.png",
"title": "News & Entertainment",
"description": "Regional & Global | News | Trending +",
"follow": 0
},
"6": {
"id": "7",
"img": "ginterests/default.png",
"title": "Celebrities",
"description": "City Events | Trending | Current Events +",
"follow": 0
},
"success": true,
"message": "Interests data sent",
"interestsCount": 7
}
解决方案
解析 JSON 对象并从中获取单个数据很容易。只需按照以下步骤操作:
- 将完整的响应存储为字符串值:
final String response= response.body().string();
- 初始化一个 JSON 对象:
JSONObject resultObject = null;
现在运行以下代码来解析 JSON 对象中的数据:`try {
resultObject = new JSONObject(response); JSONObject result = resultObject.getJSONObject(0); final String id= volumeObject.getString("id"); //repeat this step to get all the data from the JSON object } catch (JSONException e) { e.printStackTrace(); }`
推荐阅读
- php - 使用“source”命令获取提供意外输出的变量
- c# - 为什么在静态方法可以是非静态的时候使用它?
- python - 如何在无需安装 python 的情况下向用户分发 aa python 脚本
- matlab - 如何在 MatLab 中使用 ode45 修复分配错误(ode45 函数的第 488 行)
- android - 在 Android WebView 中访问 HTTP 响应标头
- perl - 如何使用 Sed 或 Perl 根据字符串的第二列号以降序方式对给定的文本文件进行排序
- c - 如何添加两个数组以产生第三个?
- class - 我怎样才能让这个调度电话工作?
- javascript - e.preventDefault() 不适用于表单提交
- entity-framework-core - 使用原始 SQL 更改数据库内容会导致 EntityFramework 上下文不同步