Json object encapsulation and parsing

Source: Internet
Author: User

1. parse the table structure information object of the json object. It is in json format and named tableObj * {* "tableName": "t_res", // table name * "columnInfo ": [// field information * {* "columnName": "id", // field name * "dataTypeName": "varchar", // field type * "isKey": true, // whether it is a primary key, true indicates a primary key, false indicates not * "isAutoIncrement": true, // whether it is auto-incrementing, true indicates auto-incrementing, false indicates not auto-increment * "isNull": 0, // whether it is null, 1 indicates it can be empty, 0 indicates it cannot be blank * "precision": 5, // precision * "defaultValue": "10" // default * "scale": 2 // decimal places *} *] *} try {String name = tableObj. get ("tableName "). toString (); // obtain the table name JSONArray columnInfo = tableObj. getJSONArray ("columnInfo"); // obtain the field description. json array int size = columnInfo. length (); // json array length for (int I = 0; I <size; I ++) {JSONObject info = columnInfo. getJSONObject (I); String cloumn = info. getString ("columnName"); // obtain the field name String dataType = info. getString ("dataTypeName"); // obtain the field type boolean isKey = info. getBoolean ("isKey"); // obtain whether the field is the primary key boolean isAutoIncrement = info. getBoolean ("isAutoIncrement"); // gets whether the field is auto-incrementing. int isNull = info. getInt ("isNull"); // get whether the field is null int precision = info. getInt ("precision"); // obtain the field type precision String defaultValue = info. getString ("defaultValue "); // obtain the default field value} 2. encapsulate the json object/*** obtain the table structure information based on the table name * @ param tableName table name * @ return table structure information object Json format * {* "tableName ": "t_res", // table name * "columnInfo": [// field information * {* "columnName": "id", // field name * "dataTypeName ": "varchar", // field type * "isKey": true, // whether it is a primary key. true indicates that it is a primary key, and false indicates that it is not * "isAutoIncrement": true, // whether the value is auto-incrementing; true indicates auto-incrementing; false indicates no auto-incrementing * "isNull": 0; // whether the value is null; 1 indicates that the value can be null, 0 indicates that the value cannot be blank * "precision": 5, // precision * "defaultValue": "10" // default * "scale ": 2 // decimal places *} *] *} */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 + ", "+" 'isautosecret': "+ autoIncrement +", "+" 'Scale': "+ scale +"} "+", "); sb. deleteCharAt (sb. length ()-1); sb. append ("]"); sb. append ("}"); // json string to complete the System. out. println (sb. toString (); tableInfoObj = new JSONObject (sb. toString (); // converts a json string to the return tableInfoObj object of jsonObject;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.