Android 如何解析结果[JSONArray]?
我正在获取JSON数据。在GridView中下载和显示图像时如何使用JSONArray。我怎样才能做到这一点?我指的是。它是JSONObject 这是我的密码Android 如何解析结果[JSONArray]?,android,json,arraylist,Android,Json,Arraylist,我正在获取JSON数据。在GridView中下载和显示图像时如何使用JSONArray。我怎样才能做到这一点?我指的是。它是JSONObject 这是我的密码 private void parseResult(String result) { try { JSONObject response = new JSONObject(result); JSONArray posts = response.optJSONArray("posts");
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("posts");
GridItem item;
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
item = new GridItem();
JSONArray img = post.getJSONArray("img");
if (null != attachments && attachments.length() > 0) {
JSONObject attachment = attachments.getJSONObject(0);
if (img!= null)
item.setImage(img.getString("url"));
}
mGridData.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是我的JSON
[
{
"id": "95",
"name": "Cherry",
"menu": [
{
"menu_id": "18",
"img": "925dd3ad6d50fa5686a82af26515be5d.jpg"
}
]
},
{...},
{...}
]
请尝试以下代码:
有关代码,请参阅:
try{
JSONObject object = new JSONObject (result);
JSONArray jsonArray = object.getJSONArray("posts");
for(int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String img_url = object.getString("url");
item = new GridItem();
if (img_url!= null)
item.setImage(img_url);
mGridData.add(item);
}
}
catch (JSONException e){
e.printStackTrace();
}
试试看{
JSONObject对象=新的JSONObject(结果);
JSONArray JSONArray=object.getJSONArray(“posts”);
对于(int i=0;i,以下是您的解决方案:
try {
JSONObject response = new JSONObject(result);
JSONArray menu = response.optJSONArray("menu");
GridItem item;
for (int i = 0; i < menu.length(); i++) {
JSONObject post = menu.optJSONObject(i);
String menu_id=post.getString("menu_id");
String img=post.getString("img");
item = new GridItem();
item.setImage(img);
mGridData.add(item);
}
}
}
试试看{
JSONObject响应=新JSONObject(结果);
JSONArray menu=response.optJSONArray(“菜单”);
网格项目;
对于(int i=0;i
请使用谷歌gson库
示例JSON字符串
{
"title": "Free Music Archive - Albums",
"message": "",
"errors": [],
"total": "11259",
"total_pages": 2252,
"page": 1,
"limit": "5",
"dataset": [
{
"album_id": "7596",
"album_title": "Album 1",
"images": [
{
"image_id": "1",
"user_id": null
}
]
}
]
}
Albums.java
class Albums {
public String title;
public String message;
public List<string> errors = new ArrayList<string>();
public String total;
public int total_pages;
public int page;
public String limit;
List<dataset> dataset = new ArrayList<dataset>();
}
哦,为什么要使用JSONArray?你没有将json字符串映射到java对象?如何解决?img.getString(“url”)我更改了image\u url,但无法解决方法getStringcheck编辑…还有两个json?你想解析哪个json图像url?org.json.JSONException:索引0超出范围[0..0)image\u url为空,我用于第二个JSON;我添加了GridItem项;在For loop.Log.i(“hello”,image\u url);hello:925dd3ad6d50fa5686a82af26515be5d.jpg之前。但它没有显示在GridView中。我认为我在另一个代码中出错了。谢谢
{
"title": "Free Music Archive - Albums",
"message": "",
"errors": [],
"total": "11259",
"total_pages": 2252,
"page": 1,
"limit": "5",
"dataset": [
{
"album_id": "7596",
"album_title": "Album 1",
"images": [
{
"image_id": "1",
"user_id": null
}
]
}
]
}
class Albums {
public String title;
public String message;
public List<string> errors = new ArrayList<string>();
public String total;
public int total_pages;
public int page;
public String limit;
List<dataset> dataset = new ArrayList<dataset>();
}
Albums albums = gson.fromJson(jsonString, Albums.class);