发布于2019-12-07 22:52 阅读(1454) 评论(0) 点赞(5) 收藏(5)
目录
注意: socket编程中传送的数据只能是基本数据类型,int float str无人船演示的时候传送个numpy形式结果出错了
用法参考下面网址:
https://blog.csdn.net/liangxy2014/article/details/78985057
注意:ndarray 数据类型有一个方法 tolist() 这个方法看似没用,这里起到了关键作用:json不能传入ndarray类型,必须传入Python原来的列表类型,这里的tolist()方法起到了关键作用,曾经试过list() 强制类型转换,不好用,这能使用这个方法。
- json.dumps(【字典或者列表类型】)
-
- #1 json化的同时具有缩进功能,indent的数值,代表缩进的位数,indent英文意思就是缩排的功能
- temp = json.dumps(【字典或者列表类型】,indent=4)
-
- #2 返回的字符串按照索引排序
- json.dumps(【字典或者列表类型】, sort_keys=True) # '{"a": 0, "b": 0, "c": 0}'
-
- #3 注意转义字符的使用
- json.dumps("\"foo\bar") # "\"foo\bar"
-
- #4 去掉多余的空格,在传输数据时候越精简越好,
- json.dumps(data, separators=(',',':'))
-
- #5 输出真正的中文需要指定ensure_ascii=False,如果不指定这个参数,那么就是ascii码,而ascii码根本就没有中文,所以出来就是看不懂的东西
- json.dumps('凉凉', ensure_ascii=False)
'''帮助文档
json. dumps ( obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False, **kw )
Serialize obj to a JSON formatted str using this conversion table. If ensure_ascii is false, the result may contain non-ASCII characters and the return value may be a unicode instance.
The arguments have the same meaning as in dump().
Note
Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings. As a result of this, if a dictionary is converted into JSON and then back into a dictionary, the dictionary may not equal the original one. That is, loads(dumps(x)) != x if x has non-string keys.
'''
-
- import json
- json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
-
- from io import StringIO
- io = StringIO('["streaming API"]')
- json.load(io) # ['streaming API']
-
-
- from io import StringIO
- io = StringIO('[1,2,3]')
- json.load(io) # [1, 2, 3]
作者:小胖子
链接:https://www.pythonheidong.com/blog/article/170254/7277556ec56c3709b874/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!