使用java将List <object>转换为JSONArray

时间:2015-06-04 09:19:12

标签: java json playframework-2.0 ebean

当我尝试将BeanList转换为JSONArray / JsonNode时,使用java play2 framework 2.3.x和Ebean ORM比使用通过循环BeanList创建的ArrayList花费更多时间并添加到该ArrayList, 让我们说当转换ArrayList所需的时间为x时,BeanList需要3x。

public class MyBean extends Model {
    private String name;
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

何时

List<MyBean> myBean =  MyBean.find.all();//will return BeanList<Device>
JSONArray myBeanJsonList = new JSONArray(myBean);

JsonNode myBeanList = Json.toJson(myBean);

比在BeanList上循环花费更多时间并在ArrayList中添加Object

    List<MyBean> myBean =  MyBean.find.all();//will return BeanList<Device>
    List<MyBean> myBeanList = new ArrayList<Device>();

    for(MyBean d : myBean)
    {
        MyBean myBeanTemp = new MyBean();
        myBeanTemp.setName(d.getName());
        myBeanList.add(myBeanTemp);
    }

    JSONArray myBeanJsonList = new JSONArray(myBeanList);

    JsonNode myBeanJsonList = Json.toJson(myBeanList);

是正常行为吗?

0 个答案:

没有答案