简体   繁体   English

Java的JSON对象

[英]JSON Object From Java

Can anyone help me how to create a JSON Object from the database. 谁能帮助我如何从数据库创建JSON对象。

This is what the JSON output should look like. 这就是JSON输出的样子。

       [{
text_layerG: 'Kanpur Analyst Roads', 
cls: 'folder',
children: [{
    text    :   'Road Major',
    layers  :   'analyst_kanpur:road_major',
    leaf    :   true,
    checked : false
},{
    text    : 'Road_minior',
    leaf    : true,
    layers  :   'analyst_kanpur:road_minor',
    checked : false
},{
    text    : 'Road_colony',
    leaf    : true,
    layers  :   'analyst_kanpur:road_colony',
    checked: false
}]

I need to populate text and layers field from database 我需要从数据库中填充文本和图层字段

this is my select Query 这是我的选择查询

String str="Select id, tablename, layername,layer_display_name,layer,visibility,isbaselayer, group_name " +
                " from layermeta " +
                " join  layer_groups on layer_group_id=group_id " +
                " where id not in (72, 73,79) " +
                " order by sequence";

where the text_layerG is group_name and 其中text_layerG是group_name,

String text=rsm.getString("layer_display_name");
            String layers=rsm.getString("layer");
            boolean leaf=true;
            boolean checked=false;
            String groupname=rsm.getString("group_name");
            JSONObject jObj = new JSONObject();
            jObj.put("text", text);
            jObj.put("layers", layers);
            jObj.put("leaf", leaf);
            jObj.put("checked",checked);
            jArray.put("children",jObj);
            JSONObject jObjDevice = new JSONObject();
            jObjDevice.put("children", jArray);

this is what i wrote and i got stuck in middle 这就是我写的,我陷入了中间

Please Help me 请帮我

Maybe JsonArray object is what you need. 也许您需要JsonArray对象。 Check this for more examples. 检查示例。

What you have done is almost correct. 您所做的几乎是正确的。 Just add the cls and text_layerG in the jObjDevice object along with children and finally add all of them in an Jsonarray. 只需在jObjDevice对象中添加clstext_layerG以及子jObjDevice ,最后将所有子jObjDevice添加到Jsonarray中。

JSONArray children = new JSONArray();
        while(rsm.next()){
            String g=rsm.getString("group_name");
            //Groups
            JSONObject gObj=new JSONObject();
            gObj.put("text", g);
            //gObj.put("checked", false);
            //gObj.put("checked", true);
            gObj.put("expanded", true);
            gObj.put("cls", "folder");
            JSONArray e=new JSONArray();
            gObj.put("children",e);
            if(groupArr.length()>0){
                boolean found=false;
                for(int i=0;i<groupArr.length();i++){
                    if(groupArr.getJSONObject(i).getString("text").equals(g)){
                        found=true;
                    }
                }
                if(!found) groupArr.put(gObj);
            }else{
                groupArr.put(gObj);
            }


            //Children
            JSONObject cl=new JSONObject();
                cl.put("text", rsm.getString("layer_display_name"));
                cl.put("layer", rsm.getString("layer"));
                cl.put("leaf", true);
                //cl.put("checked", rsm.getBoolean("visibility"));
                cl.put("checked",true);
                cl.put("group", g);
            children.put(cl);
        }

        for(int i=0;i<children.length();i++){
            JSONObject cl= children.getJSONObject(i);
            for(int j=0;j<groupArr.length();j++){
                if(cl.getString("group").equals(groupArr.getJSONObject(j).getString("text"))){
                    groupArr.getJSONObject(j).getJSONArray("children").put(cl);
                }
            }
        }

this is how i done and its working like a charm thanku friends 这就是我的工作方式,它的运作方式就像一个迷人的thanku朋友

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM