zoukankan      html  css  js  c++  java
  • json对象的封装与解析

    一、解析json对象

    表结构信息对象,json格式,名称为tableObj
      *  {
      *   "tableName":"t_res",                              //表名称
      *   "columnInfo":[           //字段信息
      *            {
      *           "columnName":"id",            //字段名
      *           "dataTypeName":"varchar",     //字段类型
      *           "isKey":true,                 //是否为主键,true代表是主键,false代表不是
      *           "isAutoIncrement":true,       //是否自增,true代表自增,false代表不自增
      *           "isNull":0,                   //是否为空,1代表可以为空,0代表不能为空
      *           "precision":5,                //精度
      *           "defaultValue":"10"           //默认值
      *           "scale":2                     //小数位数         
      *          }
      *           ]
      *  }

    try{
       String name = tableObj.get("tableName").toString();                //获得表名
       JSONArray columnInfo = tableObj.getJSONArray("columnInfo");        //获得字段描述json数组
       int size = columnInfo.length();                                    //json数组长度
       for(int i=0;i<size;i++){
        JSONObject info=columnInfo.getJSONObject(i);
        String cloumn = info.getString("columnName");                  //获取字段名
        String dataType = info.getString("dataTypeName");              //获取字段类型
        boolean isKey = info.getBoolean("isKey");                      //获取字段是否为主键
        boolean isAutoIncrement = info.getBoolean("isAutoIncrement");  //获取字段是否自增
        int isNull = info.getInt("isNull");                            //获取字段是否为空
        int precision = info.getInt("precision");                      //获取字段类型精度
        String defaultValue = info.getString("defaultValue");          //获取字段默认值

    }

    二、封装json对象


     /**
      * 根据表名获取表结构信息
      * @param tableName 表名
      * @return 表结构信息对象 Json格式
      *  {
      *   "tableName":"t_res",                              //表名称
      *   "columnInfo":[           //字段信息
      *            {
      *           "columnName":"id",            //字段名
      *           "dataTypeName":"varchar",     //字段类型
      *           "isKey":true,                 //是否为主键,true代表是主键,false代表不是
      *           "isAutoIncrement":true,       //是否自增,true代表自增,false代表不自增
      *           "isNull":0,                   //是否为空,1代表可以为空,0代表不能为空
      *           "precision":5,                //精度
      *           "defaultValue":"10"           //默认值
      *           "scale":2                     //小数位数         
      *          }
      *           ]
      *  } 
      */    

      JSONObject tableInfoObj = new JSONObject();
      StringBuffer sb = new StringBuffer();  

      sb.append("{"+"'tableName':"+tableName+","+"'columnInfo':"+"[");

      sb.append("{"+"'columnName':"+"'"+cloumn+"'"+","
          +"'dataTypeName':"+"'"+dataType+"'"+","
          +"'isKey':"+isKey+","
          +"'precision':"+precision+","
          +"'defaultValue':"+"'"+defaultValue+"'"+","
          +"'isNull':"+isNull+","
          +"'isAutoIncrement':"+autoIncrement+","
          +"'scale':"+scale+"}"+","); 

       sb.deleteCharAt(sb.length()-1);
       sb.append("]");
       sb.append("}");                                 //json字符串到此完成  
       System.out.println(sb.toString());
       tableInfoObj = new JSONObject(sb.toString());   //将json字符串转化为jsonObject对象

       return tableInfoObj;

  • 相关阅读:
    fpga配置方式 .jic固化为ps模式
    fpga新建nios
    四轴飞行器飞行原理与双闭环PID控制
    fpga为什么要用nios 开发
    error A space is required after ',' comma-spacing
    vuex : Newline required at end of file but not found eol-last
    vue -Missing space before value for key 'path'vue.js解决空格报错
    visual studio 自动补全功能 以及代码没有颜色
    hadoop 伪分布模式环境搭建
    django框架-DRF工程之认证功能
  • 原文地址:https://www.cnblogs.com/james1207/p/3292255.html
Copyright © 2011-2022 走看看