JSON是一种轻量级的数据交换格式。以完全独立于编程语言的文本格式存储和表示数据。简单和清晰的层次结构使JSON成为一种理想的数据交换语言。便于人的读写,也便于机器解析生成,能够有效提高网络传输的效率。json.dumps()是将python对象转换为json对象的过程,并生成字符串。MyEncoder来源于网络,对numpy数据类型进行转换。将numpy导入为npi导入jsonclassMyEncoder(json.JONEncoder):defdefault(self,obj):ifisinstance(obj,np.integer):returnint(obj)elifisinstance(obj,np.floating):returnfloat(obj)elifisinstance(obj,np.ndarray):returnobj.tolist()else:returnsuper(MyEncoder,self).default(obj)初始化数据,通过numpy构造随机数#初始化数据monthstr=[str(x)+'Month'forxinrange(1,13)]#['January','February','March','April','May','June','July','August','September','十月','十一月','十二月']nphigh=np.random.randint(20,35,size=12)#[213230273425273026223022]nplow=np.random.randint(5,20,size=12)#[199178149181981169]ObjectoftypendarrayisnotJSONserializable错误及解决方法#---------------errorTypeError:ObjectoftypendarrayisnotJSONserializable------------#json.dumps({'month':monthstr,'high':nphigh,'low':nplow})#--------------解决方案转码np.integer,np.floating,np.ndarrayjson.dumps({'month':米onthstr,'high':nphigh,'low':nplow},cls=MyEncoder)#{"month":["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708","11\u6708","12\u6708"],#“高”:[32、27、28、23、24、26、29、25、24、21、31、27],#“低”:[6、17、17、6、9、15、14,18,13,11,6,5]}#------------------------------------------------------------Objectoftypeint32isnotJSONserializable错误及解决方法,这里使用list()和tolist()方法,就可以了可以看到两者还是有明显区别的#list()将外层转换为list类型,而内层数据类型还是原来的类型。#tolist()方法将外层和内层转化为列表类型。#--------------------------list()和tolist()的区别-------------------------------------------#list()将外层转换为列表类型,内层数据类型为还是原来的类型。#tolist()方法将外层和内层转化为列表类型。high=list(nphigh)low=list(nplow)##--------------错误TypeError:Objectoftypeint32isnotJSONserializable--------------#json.dumps({'month':monthstr,'high':high,'low':low})#TypeError:Objectoftypeint32isnotJSONserializable#----------------解决方案对np.integer,np.floating,np.ndarray执行转码json.dumps({'month':monthstr,'high':high,'low':low},cls=MyEncoder)#{"月份":["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708","11\u6708","12\u6708"],#"high":[33,27,34,27,29,27,27,33,33,34,29,26],#"low":[17,19,11,13,7,6,6,7,15,5,13,19]}#--------------------------------------------------------------------------------------#tolist()可正常使用high=nphigh.tolist()low=nplow.tolist()json.dumps({'month':monthstr,'high':high,'low':low})#{"month":["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708",“11\u6708","12\u6708"],#"高":[29,21,22,22,31,29,21,20,32,22,20,23],#"低":[17,19,10,9,17,8,14,16,18,13,13,10]}normallist的json.dumps使用方法#normallist的使用方法high=[29,33,31,20,32,32,20,25,33,20,21,27]low=[8,12,17,8,6,17,8,17,14,9,8,18]json.dumps({'月':monthstr,'high':high,'low':low},ensure_ascii=False)#{"month":["January","February","March","April","May","June","七月","八月","九月","十月","十一月","十二月"],#"high":[29,33,31,20,32,32,20,25,33,20,21,27],#"low":[8,12,17,8,6,17,8,17,14,9,8,18]}json.dumps({'month':monthstr,'high':high,'low':low})#{"month":["1\u6708","2\u6708","3\u6708","4\u6708","5\u6708","6\u6708","7\u6708","8\u6708","9\u6708","10\u6708","11\u6708","12\u6708"],#"high":[29,33,31,20,32,32,20,25,33,20,21,27],#"低":[8,12,17,8,6,17,8,17,14,9,8,18]}
