【问题标题】:want output as json instead of array while using criteria在使用条件时希望输出为 json 而不是数组
【发布时间】:2023-03-24 18:00:01
【问题描述】:

这是我的代码,我正在使用投影来仅显示特定列,并且我将结果作为数组获取。但我希望输出为 json 我该怎么做才能将输出作为 json。

请帮帮我

public List<Educationlevel> getAllEducationlevel() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Educationlevel.class);
            ProjectionList projlist =Projections.projectionList();
            projlist.add(Projections.property("education_level"));
            projlist.add(Projections.property("education_level_id"));
            projlist.add(Projections.property("country_id"));
            criteria.add(Restrictions.eq("delete_flag", false));
            criteria.setProjection(projlist);
            return criteria.list();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.getTransaction().commit();
            session.close();
        }
    }

我得到的输出

[["PG",0,2],["PHD",1,2]]

以及我期望的输出 在这个我只想要 3 列说education_level_id,country_id,education_level

{
    education_level_id: 0
    country_id: 2
    education_level: "PG"
    delete_flag: false
    insert_by: null
    insert_date: "2015-10-19 12:49:52.0"
    update_by: null
    update_date: "2015-10-19 12:49:52.0"
}

【问题讨论】:

    标签: arrays json spring hibernate


    【解决方案1】:
    JSONObject jo = new JSONObject();    
    jo.put("education_level", "PG");    
    jo.put("education_level_id", "0");    
    JSONArray ja = new JSONArray();    
    ja.put(jo);
    

    【讨论】:

    • 我必须导入什么包才能得到您的回复
    • 我们必须导入 java-json jar 来运行它
    • stackoverflow.com/users/2849567/rugmangathan 你能推荐一些更好的 naswer...
    • 您可以在 jo.put(education_level", $level) 中动态传递您的值,其中 $level 将是您的education_level
    • 谢谢老兄......有一些想法
    猜你喜欢
    • 2017-12-21
    • 2020-08-03
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多