优化JSonArray for循环 [英] Optimize JSonArray for Loop

查看:2010
本文介绍了优化JSonArray for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我试图优化我就 Android开发者文档 JSONArray。我本来是要优化它,但有错误消息出来了。

Today, I'm trying to optimize my JSONArray regarding android developer documentation. I was supposed to optimize it but there is error message coming out.

JSONArray infojson = json.getJSONArray("getInfo");          
for(int i=0;i < infojson.length();i++){                                     
    JSONObject e = infojson.getJSONObject(i);
    hm = new HashMap<String, Object>();
    hm.put(MYNAME, e.getString("uname"));
    hm.put(MYAGE, e.getString("uage"));
}

和我上面的编码如下优化

And I've optimized above coding as follow

JSONArray infojson = jsonObject.getJSONArray("getInfo");
for (Object o : infojson ) {
    JSONObject jsonLineItem = (JSONObject) o;
    String myname = jsonLineItem.getString("uname");
    String myage = jsonLineItem.getString("uage");
}

不幸的是,我得到了以下错误消息存在的关于infojson){

只能遍历数组或java.lang.Iterable的实例

推荐答案

您不能使用JSONArray用这种语法,因为它没有实现的可迭代接口。作为一个侧面说明,微性能改善你可能不会影响作为从web服务返回的JSON结果通常不是规模大的应用程序的性能。

You can't use JSONArray with the that syntax as it doesn't implement Iterable interface. As a side note, the micro performance improvement you get probably won't affect your application performance as JSON results that are returned from the WebService are typically not large in size.

这篇关于优化JSonArray for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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