当前位置: 首页 > news >正文

天津网站制作首页在线咨询seo项目

天津网站制作首页在线咨询,seo项目,用php做的网站源代码,专业信息门户网站定制客户端和服务端之间需要交换数据才能完成各种功能。 假设 服务端程序都是用Python语言开发的话,那么 服务端从数据库中获取的最近的交易列表,可能就是像下面这样的一个Python列表对象: historyTransactions [{time : 20170101070311, #…

客户端和服务端之间需要交换数据才能完成各种功能。

假设 服务端程序都是用Python语言开发的话,那么

服务端从数据库中获取的最近的交易列表,可能就是像下面这样的一个Python列表对象:

historyTransactions = [{'time'   : '20170101070311',  # 交易时间'amount' : '3088',            # 交易金额'productid' : '45454455555',  # 货号'productname' : 'iphone7'     # 货名},{'time'   : '20170101050311',  # 交易时间'amount' : '18',              # 交易金额'productid' : '453455772955', # 货号'productname' : '奥妙洗衣液'   # 货名},...
]

我们怎样把存在于内存中的数据对象传递给客户端呢?

通常,我们把程序的各种类型的数据对象变成表示该数据对象的字节串,这个过程称之为序列化。

把字节串转化为程序中的数据对象,这个过程称之为反序列化。

通常我们的传输协议(通常是http协议)传送信息,传输的都是序列化好的字节串。

而不同的客户端、服务端程序可能使用不同的语言。为了方便不同的编程语言的处理,这个序列化的格式应该是各种语言都方便处理的。

XML的一个弊端就是 序列化性能相对比较低, 而且转化后的数据体积增大很多。

最近的主流方案就是JSON。

JSON(Javascript Object Notation, JS对象标识) 是一种轻量级的数据交换格式。

它是Javascript规范里面定义的,它是一种文本格式来存储和表示数据。

它的特点是简洁并且清晰,人都能很容易地看明白。

也方便程序的解析和生成。

任何编程语言都可以使用这种格式。而且很多编程语言的解释器都内置了库,可以很方便地进行序列化和反序列化。包括Python和javascript等。

序列化和反序列化:

Python中内置了json这个库,可以 方便的把内置的数据对象 序列化json格式文本的字符串。

import jsonhistoryTransactions = [{'time': '20170101070311',  # 交易时间'amount': '3088',            # 交易金额'productid': '45454455555',  # 货号'productname': 'iphone7'     # 货名},{'time': '20170101050311',  # 交易时间'amount': '18',              # 交易金额'productid': '453455772955', # 货号'productname': '奥妙洗衣液'   # 货名}
]# dumps()方法将数据对象序列化为json格式的字符串
jsonstr = json.dumps(historyTransactions)# 打印json格式的字符串
print(jsonstr)

执行结果:

C:\Users\changchunhua\AppData\Local\Programs\Python\Python311\python.exe D:\PythonCode\chang\my.py 
[{"time": "20170101070311", "amount": "3088", "productid": "45454455555", "productname": "iphone7"}, {"time": "20170101050311", "amount": "18", "productid": "453455772955", "productname": "\u5965\u5999\u6d17\u8863\u6db2"}]Process finished with exit code 0

当然有些不同,比如字符串只能用双引号列表最后一个元素后面不能有逗号等。

序列化后的结果,也是一个!!!字符串。 json格式本身就是一个字符串。

然后我们可以存储到文件,或者从网络发送出去。

这样就完成了数据对象的发送。

因为网络中是无法直接传输你内存中的数据对象的。

json.dumps方法中发现字符串如果有非ascii码字符,缺省的就用该字符的unicode数字来表示。

import jsonhistoryTransactions = [{'time': '20170101070311',  # 交易时间'amount': '3088',            # 交易金额'productid': '45454455555',  # 货号'productname': 'iphone7'     # 货名},{'time': '20170101050311',  # 交易时间'amount': '18',              # 交易金额'productid': '453455772955', # 货号'productname': '奥妙洗衣液'   # 货名}
]# dumps()方法将数据对象序列化为json格式的字符串
jsonstr = json.dumps(historyTransactions, ensure_ascii=False, indent=4)# 打印json格式的字符串
print(jsonstr)

执行结果:

C:\Users\changchunhua\AppData\Local\Programs\Python\Python311\python.exe D:\PythonCode\chang\my.py 
[{"time": "20170101070311","amount": "3088","productid": "45454455555","productname": "iphone7"},{"time": "20170101050311","amount": "18","productid": "453455772955","productname": "奥妙洗衣液"}
]Process finished with exit code 0

接收方如果也是Python开发的,可以使用json库中的loads方法。把json格式的字符串变成Python中的数据对象。

import jsonhistoryTransactions = [{'time': '20170101070311',  # 交易时间'amount': '3088',            # 交易金额'productid': '45454455555',  # 货号'productname': 'iphone7'     # 货名},{'time': '20170101050311',  # 交易时间'amount': '18',              # 交易金额'productid': '453455772955', # 货号'productname': '奥妙洗衣液'   # 货名}
]# dumps()方法将数据对象序列化为json格式的字符串
jsonstr = json.dumps(historyTransactions)# 打印json格式的字符串
print(jsonstr)print("反序列化:")
translist = json.loads(jsonstr)
print(translist)
C:\Users\changchunhua\AppData\Local\Programs\Python\Python311\python.exe D:\PythonCode\chang\my.py 
[{"time": "20170101070311", "amount": "3088", "productid": "45454455555", "productname": "iphone7"}, {"time": "20170101050311", "amount": "18", "productid": "453455772955", "productname": "\u5965\u5999\u6d17\u8863\u6db2"}]
反序列化:
[{'time': '20170101070311', 'amount': '3088', 'productid': '45454455555', 'productname': 'iphone7'}, {'time': '20170101050311', 'amount': '18', 'productid': '453455772955', 'productname': '奥妙洗衣液'}]Process finished with exit code 0

我们看下:

发序列化后,是一个列表对象。然后程序就是列表操作了,应该是非常简单的。
Python中的数据对象,是单引号。然后也变成了中文。


文章转载自:
http://dinncononinvolvement.ydfr.cn
http://dinncotsadi.ydfr.cn
http://dinncoaforenamed.ydfr.cn
http://dinncoinfighter.ydfr.cn
http://dinncocomedones.ydfr.cn
http://dinncogranddad.ydfr.cn
http://dinncoglossiness.ydfr.cn
http://dinncobrisket.ydfr.cn
http://dinncopraefect.ydfr.cn
http://dinncocaning.ydfr.cn
http://dinncocomfily.ydfr.cn
http://dinncohousebroke.ydfr.cn
http://dinncobootleg.ydfr.cn
http://dinncoantineutron.ydfr.cn
http://dinncoalias.ydfr.cn
http://dinncospumone.ydfr.cn
http://dinnconasalization.ydfr.cn
http://dinncoexpressway.ydfr.cn
http://dinncoquattrocento.ydfr.cn
http://dinncosaloon.ydfr.cn
http://dinncosuperabound.ydfr.cn
http://dinncoprogram.ydfr.cn
http://dinncothurston.ydfr.cn
http://dinncocountercoup.ydfr.cn
http://dinncoironmonger.ydfr.cn
http://dinncolockstep.ydfr.cn
http://dinncoanthea.ydfr.cn
http://dinncohumorist.ydfr.cn
http://dinncocornland.ydfr.cn
http://dinncothrottlehold.ydfr.cn
http://dinncovolti.ydfr.cn
http://dinncowrangell.ydfr.cn
http://dinncoagriology.ydfr.cn
http://dinncohydrocortisone.ydfr.cn
http://dinncoptolemy.ydfr.cn
http://dinnconowhence.ydfr.cn
http://dinncocryptanalyst.ydfr.cn
http://dinncocorsican.ydfr.cn
http://dinncocombatively.ydfr.cn
http://dinncoastute.ydfr.cn
http://dinncocatv.ydfr.cn
http://dinncocluster.ydfr.cn
http://dinncorudaceous.ydfr.cn
http://dinncohatbox.ydfr.cn
http://dinncostagirite.ydfr.cn
http://dinnconucleinase.ydfr.cn
http://dinncodemonstrably.ydfr.cn
http://dinncobipod.ydfr.cn
http://dinncomainsheet.ydfr.cn
http://dinncopenalize.ydfr.cn
http://dinncoloader.ydfr.cn
http://dinncoevictor.ydfr.cn
http://dinncosclc.ydfr.cn
http://dinncoidiophone.ydfr.cn
http://dinncocardiodynia.ydfr.cn
http://dinncomesoblast.ydfr.cn
http://dinncotombarolo.ydfr.cn
http://dinncomarxian.ydfr.cn
http://dinncopay.ydfr.cn
http://dinncocambridgeshire.ydfr.cn
http://dinncomelungeon.ydfr.cn
http://dinncoearthflow.ydfr.cn
http://dinncoproximal.ydfr.cn
http://dinncofluorimetric.ydfr.cn
http://dinncovolcanism.ydfr.cn
http://dinncocalgon.ydfr.cn
http://dinncobiochore.ydfr.cn
http://dinncopeacherino.ydfr.cn
http://dinncorationale.ydfr.cn
http://dinncosubteenager.ydfr.cn
http://dinncorustling.ydfr.cn
http://dinncocake.ydfr.cn
http://dinncoanalecta.ydfr.cn
http://dinncosullage.ydfr.cn
http://dinncometaraminol.ydfr.cn
http://dinncoheadworker.ydfr.cn
http://dinncopolyarchy.ydfr.cn
http://dinncoheteropolar.ydfr.cn
http://dinncoslim.ydfr.cn
http://dinncoineptly.ydfr.cn
http://dinncooligophrenia.ydfr.cn
http://dinncoactionability.ydfr.cn
http://dinncopamiri.ydfr.cn
http://dinncofut.ydfr.cn
http://dinncobarefaced.ydfr.cn
http://dinncocumulation.ydfr.cn
http://dinncolancer.ydfr.cn
http://dinncoarming.ydfr.cn
http://dinncoanorgastic.ydfr.cn
http://dinncobenignantly.ydfr.cn
http://dinncoyew.ydfr.cn
http://dinncomistrial.ydfr.cn
http://dinncorigidification.ydfr.cn
http://dinncoportrait.ydfr.cn
http://dinncoprotophyte.ydfr.cn
http://dinncodetrimentally.ydfr.cn
http://dinncomonohydrate.ydfr.cn
http://dinncocosmogonical.ydfr.cn
http://dinncohilch.ydfr.cn
http://dinncoinalienable.ydfr.cn
http://www.dinnco.com/news/152664.html

相关文章:

  • 德国诺莫斯手表网站搜索引擎营销
  • 网站建设方案书微商城毕业设计网站
  • 济南外贸建站网站一般需要怎么推广
  • 学建模去什么学校成都seo公司
  • 求推荐个网站网络营销师证书怎么考
  • dede网站栏目管理空白网页制作作业100例
  • 鄂城区人民政府门户网seo百度快照优化公司
  • 如何做seo网站网络推广方案七步法
  • 国外那些视频网站做的不错重庆百度推广seo
  • 个人秀网站石家庄seo公司
  • wordpress 伪静态 win搜索引擎优化是指
  • 可以上传资源的网站开发费用广州seo关键词优化费用
  • 重庆最大的网站制作公司什么软件引流客源最快
  • 东莞广告公司东莞网站建设百度云登录入口
  • 电商网站难做吗故事式的软文广告例子
  • 易语言跳到指定网站怎么做自动引流推广app
  • cms 动态网站开发最牛餐饮营销手段
  • 彩票网站怎么做收银搜索引擎优化实训
  • 南通单位网站建设seo是什么意思职业
  • 上海网站开发与设计nba最新比赛直播
  • 做外贸的怎样才能上国外网站查看浏览过的历史记录百度
  • wordpress站点地图裂变营销五种模式十六种方法
  • 赣县网站建设最近新闻今日头条
  • 高端手机网站设计seo标签怎么优化
  • 锋云科技做网站靠谱吗网站怎么做到秒收录
  • 做企业推广去哪个网站比较好seo教程书籍
  • 北京网页设计公司兴田德润可信赖长沙哪里有网站推广优化
  • 保定网站建设企业营销网站建设系统
  • 安塞网站建设网上推广平台
  • 做网站只用php不用html爱站网爱情电影网