From a7c76bad6f30d7573da8d8c3665f4aba4ac9d8bd Mon Sep 17 00:00:00 2001 From: walker Date: Fri, 20 Oct 2017 11:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0json=E8=BD=AC=E5=AE=9E?= =?UTF-8?q?=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.json | 53 ++++++++++++++++++++++++++++++ json2oc.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 data.json create mode 100644 json2oc.js diff --git a/data.json b/data.json new file mode 100644 index 0000000..2f7b34e --- /dev/null +++ b/data.json @@ -0,0 +1,53 @@ +{ + "agreementUuid": "c6e32f8e-c9da-4aa9-bed7-4bccc21ada02", + "agreementApplyUuid": "4ea7cdc2-b45e-4285-a397-192759daa1ed", + "serialNumber": "20171009110559", + "expertId": "ffebd7d9-eb8d-44cb-95fc-956ddcc56295000", + "expertUserId": 123123145883, + "expertName": "龚雨程医", + "hospitalId": "ED25EA3F3F52A102E040A8C00F01221B000", + "hospitalName": "浙江萧山医院", + "provinceId": 24, + "provinceName": "浙江", + "cityId": 552, + "cityName": "杭州", + "districtId": 3395, + "districtName": "萧山区", + "patientUserId": 2016811232127682, + "patientId": 75086705, + "patientName": "雨程患六", + "patientIdCard": "330104198010090024", + "patientBirthday": 339868800000, + "patientSex": 2, + "patientMobile": "15800000006", + "patientImg": "", + "patientCardType": 1, + "patientCardNo": "1234567890", + "patientProvinceId": 24, + "patientProvinceName": "浙江", + "patientCityId": 552, + "patientCityName": "杭州", + "patientDistrictId": 3395, + "patientDistrictName": "萧山区", + "patientAddress": "雨程患六常驻地址", + "patientSignature": "", + "serviceYear": "364个月", + "serviceCount": 26, + "nextSignNoticeDays": 0, + "signTime": 1507518415000, + "effectiveTime": 1483200000000, + "invalidTime": 1514649600000, + "isFollow": 1, + "isDefault": 1, + "remark": "", + "sourceId": "460", + "status": 1, + "gmtCreated": 1507518415000, + "gmtModified": 1508298163000, + "businessType": 1, + "censusRegisterAddress": "雨程患六户籍地址", + "fixTelephone": "0575", + "patientMobileBelonger": "龚雨程", + "serviceTimeUnit": "d", + "groupUuid": "689ff3b0-f198-4e50-bbc6-c6fb048dc5ed" + } \ No newline at end of file diff --git a/json2oc.js b/json2oc.js new file mode 100644 index 0000000..dbc7628 --- /dev/null +++ b/json2oc.js @@ -0,0 +1,94 @@ +let fs = require('fs-extra'), + path = require('path'), + strlist = 'list', + intlist = 'list'; + +(async () => { + +let data = await readFile("data.json"); +let json = JSON.parse(data); +console.log("//========start========") +parsejson(json); +})().catch(console.log); + +function parsejson(json, isRecursive) { + for (let key in json) { + let tplstr = "`@property (nonatomic, ${hold}) ${type}${name};`", + hold = "copy", + type = "NSString *", + name = key, + value = json[key]; + if(isRecursive) tplstr = "`// @property (nonatomic, ${hold}) ${type}${name};`"; + if(isInt(value)){ + hold = "assign"; + type = "NSInteger " + console.log(eval(tplstr)); + }else if(isFloat(value)){ + hold = "assign"; + type = "CGFloat " + console.log(eval(tplstr)); + }else{ + if(typeof value == 'object' && value != null) { + // 碰到数组或对象, 执行一次子递归 + if(isArray(value)){ + console.log(`//-------数组: ${key} 开始`); + parsejson(value[0], true); + }else { + console.log(`//-------对象: ${key} 开始`); + parsejson(value, true); + } + console.log(`//-------${key} 结束`) + }else{ + console.log(eval(tplstr)); + } + } + } +} + +async function readFile(filename) { + let fullpath = path.resolve(filename); + return await fs.readFile(fullpath, 'utf8').catch(console.log); +} + +function isInt(n){ + return Number(n) === n && n % 1 === 0; +} + +function isFloat(n){ + return Number(n) === n && n % 1 !== 0; +} + +function isArray(n){ + return Object.prototype.toString.call(n) === '[object Array]' +} + +// async function json_to_code () { +// let copyright = "WeDoctor Group", +// projectname = "", +// author = "walker", +// model_path = "templates/model", +// task_path = "templates/httpclient", +// // 模板内容 +// h_content = await getFileContent(model_path, 'template.h'), +// m_content1 = await getFileContent(model_path, 'template.m'), +// m_content2 = await getFileContent(model_path, 'templatebase.m'), +// h_task = await getFileContent(task_path, 'task.h'), +// m_task = await getFileContent(task_path, 'task.m'); +// let out_model = "output_model_single", +// exist_file = []; // 重复定义的类, 只生成一次, 生成过一次就丢到这个数组里打标 +// await fs.emptyDir(out_model); // 创建/清空输出文件夹 +// entities.forEach(async (model, index) => { +// // 有些子类字段是一样的, 我们写脚本的时候把类名写成一样的, 此处会 pass 掉 +// // 但只对单次执行脚本有效 +// if(classCollect.filter(m=>m==model.className).length>1) { +// if(exist_file.includes(model)) return; +// exist_file.push(model); +// } +// let m_content = model.isRoot ? m_content2 : m_content1; +// // 输出路径 +// let h_file = getPath(out_model, model.className+'.h'), +// m_file = getPath(out_model, model.className+'.m'); +// await renderFile(h_file, eval(h_content)).catch(console.log); +// await renderFile(m_file, eval(m_content)).catch(console.log); +// }); +// } \ No newline at end of file