前言
- 当我们在将网络请求回来的数据进行OC模型的创建的时候,常常会有大量的重复的机械的拷贝
{
"code": 0,
"message": "",
"result": {
"aid": "be3bdab8-fbf5-4e89-97cb-56b00048b09b",
"audios": [],
"avatar_url": "https://www.google.com/a.jpg",
"call_price": 0,
"cid": "efad6549-be62-40d7-a425-40f9b7730192",
"cover_url": "https://www.google.com/a.jpg",
"created_on": "1528349104",
"fields": [
{
"key": "name",
"value": "Mr.Wang"
},
{
"key": "company",
"value": "TicTalk"
},
{
"key": "position",
"value": "Senior Engineer"
}
],
"geo": {
"description": "Shanghai, China",
"latitude": 48.82694828196076,
"longitude": 2.367038433387592
},
"id": 2,
"message_price": 0,
"photo_sets": [],
"pid": "9b64395f-687c-4f34-9a5d-68d9adafa4cc",
"rid": "578ff973-c707-42b2-bfc2-87e4dc8e0efd",
"role_open": false,
"sid": "8f560338-dfa5-48be-b44c-65fd87798543",
"status": 0,
"updated_on": "1528349104",
"video_price": 0,
"videos": [],
"world_open": true
}
}
准备工作
- 生成一个Xcode的工程或者从随便一个工程拷贝一个Xocde的生成文件
- 拷贝到桌面 - 并且转为html的格式
- 使用打开方式为Xcode的打开方式 拷贝以下代码
<html>
<head>
<meta charset="utf-8"/>
<title>
</title>
</head>
<body>
<div style="text-align: center;">
<textarea id="textarea" style="width: 600px;height: 250px;" placeholder="请输入json"></textarea>
</div>
<div style="text-align: center;">
<button onclick="start()" style="width: 100px;height: 30px">转oc</button>
</div>
<div id="result" style="margin: 0 auto;width: 600px;font-size: 22px"></div>
</body>
<script type="text/javascript">
var gettype=Object.prototype.toString;
String.prototype.firstUpperCase = function(){
return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
return $1.toUpperCase() + $2.toLowerCase();
});
}
function start(){
try{
var result = JSON.parse(document.getElementById("textarea").value);
}catch(error){
console.log("解析JSON文件失败:"+error);
return;
}
if (!result) {
console.log("解析JSON文件无效");
return;
}
document.classArray = new Array();
parseObject("MyObject",result);
var stringAll="";
for (var i = document.classArray.length-1; i >=0 ; i--) {
let cla = document.classArray[i];
stringAll+=" \r\n\r\n@interface "+cla.name+" : NSObject\r\n\r\n";
console.log(" \r\n\r\n@interface "+cla.name+" : NSObject\r\n\r\n");
for (var j = 0; j < cla.property.length; j++) {
stringAll+=cla.property[j]+"\r\n\r\n";
console.log(cla.property[j]+"\r\n\r\n");
}
stringAll+="@end\r\n\r\n";
console.log("@end\r\n\r\n");
}
stringAll+="===========m文件======================\r\n";
console.log("===========m文件======================\r\n");
for (var i = document.classArray.length-1; i >=0 ; i--) {
let cla = document.classArray[i];
stringAll+="@implementation "+cla.name+"\r\n\r\n@end\r\n\r\n";
console.log("@implementation "+cla.name+"\r\n\r\n@end\r\n\r\n");
}
document.getElementById("result").innerText = stringAll;
}
function parseObject(k,result){
let c = new Class(k);
document.classArray.push(c);
for (var i = 0; i < Object.getOwnPropertyNames(result).length; i++) {
let key = Object.getOwnPropertyNames(result)[i];
let value = result[key];
let type = getType(value);
if(type==null){
continue;
}
if (type=="Object") {
if (Object.getOwnPropertyNames(value).length==0) {
c.property.push("@property(nonatomic, strong) NSDictionary *"+key+";");
}else{
parseObject(key.firstUpperCase(),value);
c.property.push("@property(nonatomic, strong) "+key.firstUpperCase()+" *"+key+";");
}
continue;
}
if (type=="Array") {
if (value.length>0) {
let obj = value[0];
let t = getType(obj);
if (t==null) {
continue;
}
if (t=="Object") {
c.property.push("@property(nonatomic, strong) NSArray"+" *"+key+";");
parseObject(key.firstUpperCase(),obj);
}else{
c.property.push("@property(nonatomic, strong) NSArray<"+t+"*>*"+key+";");
}
}else{
c.property.push("@property(nonatomic, strong) NSArray *"+key+";");
}
continue;
}
if (type=="id") {
c.property.push("@property(nonatomic, strong) "+type+""+key+";");
continue;
}
c.property.push("@property(nonatomic, strong) "+type+" *"+key+";");
}
}
function getType(obj){
if (typeof obj == 'number') {
return "NSNumber";
}
if (typeof obj == 'undefined') {
return "id";
}
if (typeof obj == 'null') {
return "id";
}
if (typeof obj == 'function') {
return null;
}
if (typeof obj == 'string') {
return "NSString"
}
if (typeof obj == 'boolean') {
return "NSNumber"
}
if (typeof obj == 'object') {
if (gettype.call(obj)=="[object Object]") {
return "Object";
}
if (gettype.call(obj)=="[object Array]") {
return "Array";
}
if (gettype.call(obj)=="[object Null]"){
return "id";
}
}
}
function Class(name){
this.name = name;
this.property = new Array();
}
</script>
</html>
使用
在浏览器打开 - ViewController.html
将json粘贴进去
- 这里有个小bug - 最后一个json字段要去掉逗号,解析完成后如下: