Json object from database in java

前端 未结 4 574
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 14:53

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

This is what the JSON output should look like:

{“devicelist”:{
    “de         


        
4条回答
  •  难免孤独
    2021-02-04 15:06

    If you want to extract the data from the DB and construct the JSON Object yourself, you can do:

    JsonArray jArray = new JsonArray();
    while (result.next())
    {
        String  type_json=result.getString("type");
        String name_json=result.getString("name");
        String id_json=result.getString("demo");
        JsonObject jObj = new JsonObject();
        jobj.put("id", id_json);
        jobj.put("type", type_json);
        jobj.put("name", name_json);
        jArray.put(jObj);
    }
    
    JsonObject jObjDevice = new JsonObject();
    jObjDevice.put("device", jArray);
    JsonObject jObjDeviceList = new JsonObject();
    jObjDevice.put("devicelist", jObjDevice );
    

    now jObjDeviceList contains all the data.

提交回复
热议问题