JSONObject 的一些常见用法

ResultSet转JsonObject对象

    private static JSONObject rs2JsonObject(ResultSet rs, String key0) {
        ResultSetMetaData metaData;
        try {
            metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();

            JSONObject jsonObj = new JSONObject();

            // 遍历每一列
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnLabel(i);
                String value = rs.getString(columnName);
                jsonObj.put(columnName, value);
            }
            return jsonObj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

创建一个JSONObject对象并转为字符串

JSONObject jsonStr=new JSONObject();
jsonStr.put("somekey", 'somevalue');
jedis.set(key, jsonStr.toString());

字符串转JSONObject对象

JSONObject newModel=new JSONObject(gsonStr);
int record_count=newModel.getInt("record_count")+1;
newModel.put("record_count", record_count);

你可能感兴趣的:(JSONObject 的一些常见用法)