android - 如何使用 volley 在 android studio 中获取 Json 数组数据?
问题描述
我有一个 JSON 编码数组,如下所示:-
{
"imager": [{
"title": "Guru",
"images": ["images\/6.png", "images\/androidIntro.png", "images\/barretr_Earth.png"]
}]
}
我的问题是我想一张一张地从图像数组中获取所有图像,以便我可以在 imageview 上显示图像。我的主要目标是只显示一次标题并显示与标题相关的所有图像,我已经在整个互联网和堆栈流中进行了搜索,但我无法找到正确的答案有人可以帮我解决这个问题吗?我正在使用 Volley Libabry 这是我的代码:-
url = "myurl";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("imager");
for (int i=0; i<jsonArray.length(); i++)
{
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String images = object.getString("images");
// what to do here now?? help me?
}
如果我设置
textview.setText(images);
output will be like this:-
["images\/6.png","images\/androidIntro.png","images\/barretr_Earth.png"]
但我只想要图像/6.png 图像/androidIntro.png 图像/battetr_Earth.png
这样我就可以在 imageview 中显示这些所有图像。
解决方案
您必须将"images"
标签作为数组处理。
代替
String images = object.getString("images");
利用
JsonArray images = object.getJSONArray("images");
for (int j=0; j<images.length(); j++) {
String image = images.getString(j)
// image will be
// j = 0 -> "images\/6.png"
// j = 1 -> "images\/androidIntro.png"
// j = 2 -> "images\/barretr_Earth.png"
}
推荐阅读
- python - 块 sdk 交易错误 - 无效金额
- file - SuiteScript - 打开、读取文件
- python - 如何在提取色矩特征时使偏度值不为空?
- reactjs - 如何在 React 高阶组件中输入 defaultProps
- firebase - 颤动 Firestore 数据检查并导航到新屏幕
- c# - 我无法让 AspNetCoreRateLimit 限制 API 调用
- restriction - 无法将楼层限制添加到 My Miconic PDDL 域代码
- python - Anaconda /Miniconda 导航器不工作我试图安装但无法安装,缺少脚本文件
- keras-tuner - 如何在 keras 调谐器中选择模型以外的最佳模型?
- python - Office365 IMAP 有时无法获取新电子邮件