JSON Object From Java

Can anyone help me how to create a JSON Object from the database.

This is what the JSON output should look like.

       [{
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

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

Answers


Maybe JsonArray object is what you need. 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.


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


Need Your Help

trouble with jquery load method

jquery

i have a table in one html file and div in another and both files are on same server, same folder. I'm trying to use jquery load function to do the folowing. So I do :

Skip multiple iterations in loop python

python loops iterator next continue

I have a list in a loop and I want to skip 3 elements after look has been reached.