如何解析JSON在Android的数组(不是JSON对象) [英] How to parse JSON Array (Not Json Object) in Android
问题描述
我有一个很难找到一种方法如何解析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,现在我需要做的是为Android,不能算出它做一个iPhone应用程序。 有很多的例子摆在那里,但他们都是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?
许多AP preciated!
Much appreciated !
推荐答案
使用下面的代码片段解析JsonArray。
use the following snippet to parse the JsonArray.
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
希望它帮助。
Hope it helps.
这篇关于如何解析JSON在Android的数组(不是JSON对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!