如何在 Android 中解析 JSON 数组(不是 Json 对象) [英] How to parse JSON Array (Not Json Object) in Android

查看:32
本文介绍了如何在 Android 中解析 JSON 数组(不是 Json 对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到解析 JSONArray 的方法.它看起来像这样:

I have a trouble finding a way how to parse JSONArray. It looks like this:

[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]

如果 JSON 的编写方式不同,我知道如何解析它(换句话说,如果我返回的是 json 对象而不是对象数组).但这就是我所拥有的,并且必须随它去.

I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects). But it's all I have and have to go with it.

*这是一个有效的 json.我使用这个 json 制作了一个 iPhone 应用程序,现在我需要为 Android 做它并且无法弄清楚.那里有很多示例,但它们都与 JSONObject 相关.我需要一些 JSONArray 的东西.

* It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out. There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.

有人可以给我一些提示,或者教程或示例吗?

Can somebody please give me some hint, or a tutorial or an example?

非常感谢!

推荐答案

使用以下代码片段解析 JsonArray.

use the following snippet to parse the JsonArray.

JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String name = jsonobject.getString("name");
    String url = jsonobject.getString("url");
}

这篇关于如何在 Android 中解析 JSON 数组(不是 Json 对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆