import os
# import handle_files.LocalReader as file_reader
ROOT_PATH='/Users/e101144/Desktop/Project/py_tool/readFile'
# TXT_HANDLE = file_reader.OpFile(rootPath=ROOT_PATH)
def readJson(file_path):
'''读取指定json文件路径返回json'''
# 指定json文件路径
# file_path = "/path/to/your/file.json"
# 打开并读取json文件
with open(file_path, 'r') as file:
data = json.load(file)
# 打印读取的数据
return data
def writeToFile(filePath,content):
'''将content存在入指定文件,content是list'''
os.makedirs(os.path.dirname(filePath), exist_ok=True)
with open(filePath, 'w') as file:
for item in content:
file.write("%s" % item)
file.close()
def createFile():
# json 文件
file_name = 'testdata'
file_path = '/ready/{}.json'.format(file_name)
# Open the JSON file
# data = TXT_HANDLE.readJson(fileName=file_path)
data = readJson(file_path=ROOT_PATH+file_path)
# Generate Objective-C classes for the objects in the JSON file
name = file_name.capitalize()
create_file_name = '"CC'+name+'.h"\n\n'
res_content_h = ['#import <Foundation/Foundation.h>\n\n']
res_content_m = ['#import {}\n\n'.format(create_file_name)]
# 掉用递归函数处理各个key-value
recursiveJsonData(data,name,res_content_h,res_content_m)
# 存储文件
# TXT_HANDLE.save(subPath='/a_res/a_tool_res/',fileName='CC'+name+'.h',conentList=res_content_h)
# TXT_HANDLE.save(subPath='/a_res/a_tool_res/',fileName='CC'+name+'.m',conentList=res_content_m)
writeToFile(filePath=ROOT_PATH+'/a_res/a_tool_res/'+'CC'+name+'.h',content=res_content_h)
writeToFile(filePath=ROOT_PATH+'/a_res/a_tool_res/'+'CC'+name+'.m',content=res_content_m)
# 递归函数处理各个key-value
def recursiveJsonData(data,name,res_content_h,res_content_m):
'''递归函数处理各个key-value'''
className = name
res_content_h.insert(1,'@class {};\n'.format(className))
header = '@interface {} : NSObject\n'.format(className)
end = '@end\n'
res_content_h.append(header)
res_content_h.append(end)
header_m = '@implementation {}\n\n@end\n'.format(className)
res_content_m.append(header_m)
for item in data:
value = data[item]
if type(value) is dict:
# print('dict:'+str(value))
index = res_content_h.index(header)+1
content = '@property (nonatomic, strong) {} *{};\n'.format(className+item.capitalize()+'Obj', item)
res_content_h.insert(index,content)
recursiveJsonData(value,className+item.capitalize()+'Obj',res_content_h,res_content_m)
elif type(value) is list:
# print('list:'+str(value))
index = res_content_h.index(header)+1
content = '@property (nonatomic, strong) NSMutableArray<{}*> *{};\n'.format(className+item.capitalize()+'Item', item)
res_content_h.insert(index,content)
recursiveJsonData(value[0],className+item.capitalize()+'Item',res_content_h,res_content_m)
else:
content = '\n'
index = res_content_h.index(header)+1
if type(value) is str:
content = '@property (nonatomic, copy) NSString *{};\n'.format(item)
elif type(value) is int:
content = '@property (nonatomic, assign) int {};\n'.format(item)
elif type(value) is float:
content = '@property (nonatomic, assign) CGFloat {};\n'.format(item)
elif type(value) is bool:
content = '@property (nonatomic, assign) BOOL {};\n'.format(item)
res_content_h.insert(index,content)
return
# 主函数 执行代码
def main():
createFile()
print('pass')
if __name__ == '__main__':
main()```
网友评论