如何在 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");
}