本文共 5791 字,大约阅读时间需要 19 分钟。
(JavaScript Object Notation) 是一种以人可读的文本表示对象的方法。 它已经变成 NoSQL 世界交换数据的事实标准。当一个对象被序列化成为 JSON,它被称为一个 JSON 文档 。由于 JSON 数据格式比较简单, 易于读写, 格式都是压缩的, 占用带宽小,易于解析等特点,使得它在各种场合下都十分受欢迎,客户端 JavaScript 可以简单的通过 eval() 进行JSON数据的读取,包括 Java,JavaScript,Perl,PHP,Python,Ruby等服务器端语言(几乎大部分语言都支持对 json文件的解析), 便于服务器端的解析。
但是在相同情况下,与 json 相同内容的 yaml 文件却更加简洁,我们先来看看样例(两个文件表达的内容一样):
# test.json{ "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "pod-redis", "labels": { "name": "redis" } }, "spec": { "restartPolicy": "Always", "nodeSelector": { "zone": "node1" }, "containers": [ { "name": "pod-redis", "image": "docker.io/redis", "imagePullPolicy": "Never", "ports": [ { "containerPort": 6379, "hostPort": 8080 } ] } ] }}
# test.yamlapiVersion: v1kind: Podmetadata: name: pod-redis labels: name: redisspec: restartPolicy: Always nodeSelector: zone: node1 containers: - name: pod-redis image: docker.io/redis imagePullPolicy: Never ports: - containerPort: 6379 hostPort: 8080
yaml 和 json 格式文件的读取:
# Rubyrequire 'json'require 'yaml'# 解析出来的都是 Ruby 中的 Hash 对象# puts JSON.parse(File.read('test.json'))puts JSON.load(File.open('test.json'))# puts YAML.load_file("test.yaml")puts YAML.load(File.open("test.yaml"))------------------------------------------------------------D:\Ruby23-x64\bin\ruby.exe D:/MyProject/Ruby/workspace/test.rb{"apiVersion"=>"v1", "kind"=>"Pod", "metadata"=>{"name"=>"pod-redis", "labels"=>{"name"=>"redis"}}, "spec"=>{"restartPolicy"=>"Always", "nodeSelector"=>{"zone"=>"node1"}, "containers"=>[{"name"=>"pod-redis", "image"=>"docker.io/redis", "imagePullPolicy"=>"Never", "ports"=>[{"containerPort"=>6379, "hostPort"=>8080}]}]}}{"apiVersion"=>"v1", "kind"=>"Pod", "metadata"=>{"name"=>"pod-redis", "labels"=>{"name"=>"redis"}}, "spec"=>{"restartPolicy"=>"Always", "nodeSelector"=>{"zone"=>"node1"}, "containers"=>[{"name"=>"pod-redis", "image"=>"docker.io/redis", "imagePullPolicy"=>"Never", "ports"=>[{"containerPort"=>6379, "hostPort"=>8080}]}]}}Process finished with exit code 0
hash object 的 pretty print :
# Rubyrequire 'json'require 'yaml'require 'pp'pp JSON.load(File.open('test.json'))putspp YAML.load(File.open("test.yaml"))------------------------------------------------------------D:\Ruby23-x64\bin\ruby.exe D:/MyProject/Ruby/workspace/test.rb{"apiVersion"=>"v1", "kind"=>"Pod", "metadata"=>{"name"=>"pod-redis", "labels"=>{"name"=>"redis"}}, "spec"=> {"restartPolicy"=>"Always", "nodeSelector"=>{"zone"=>"node1"}, "containers"=> [{"name"=>"pod-redis", "image"=>"docker.io/redis", "imagePullPolicy"=>"Never", "ports"=>[{"containerPort"=>6379, "hostPort"=>8080}]}]}}{"apiVersion"=>"v1", "kind"=>"Pod", "metadata"=>{"name"=>"pod-redis", "labels"=>{"name"=>"redis"}}, "spec"=> {"restartPolicy"=>"Always", "nodeSelector"=>{"zone"=>"node1"}, "containers"=> [{"name"=>"pod-redis", "image"=>"docker.io/redis", "imagePullPolicy"=>"Never", "ports"=>[{"containerPort"=>6379, "hostPort"=>8080}]}]}}Process finished with exit code 0
yaml 和 json 格式文件的读取:
import jsonimport yamlwith open('test.yaml', 'r', encoding='utf-8') as f: yaml_str = f.read()dict_obj = yaml.load(yaml_str, yaml.FullLoader)print(dict_obj)# json.load(open('test.json', 'rb'))# or# with open("centos.json", 'rb') as f:# dict_obj = json.load(f)with open("test.json") as f: json_str = f.read()dict_obj = json.loads(json_str)print(dict_obj)------------------------------------------------------------"D:\Program Files\Python36\python3.exe" D:/MyProject/Python/workspace/test.py{'apiVersion': 'v1', 'kind': 'Pod', 'metadata': {'name': 'pod-redis', 'labels': {'name': 'redis'}}, 'spec': {'restartPolicy': 'Always', 'nodeSelector': {'zone': 'node1'}, 'containers': [{'name': 'pod-redis', 'image': 'docker.io/redis', 'imagePullPolicy': 'Never', 'ports': [{'containerPort': 6379, 'hostPort': 8080}]}]}}{'apiVersion': 'v1', 'kind': 'Pod', 'metadata': {'name': 'pod-redis', 'labels': {'name': 'redis'}}, 'spec': {'restartPolicy': 'Always', 'nodeSelector': {'zone': 'node1'}, 'containers': [{'name': 'pod-redis', 'image': 'docker.io/redis', 'imagePullPolicy': 'Never', 'ports': [{'containerPort': 6379, 'hostPort': 8080}]}]}}Process finished with exit code 0
json_str object 的 pretty print :
import jsonimport yamlwith open('test.yaml', 'r', encoding='utf-8') as f: yaml_str = f.read()dict_obj = yaml.load(yaml_str, yaml.FullLoader)print(json.dumps(dict_obj, indent=2))print()with open("test.json") as f: json_str = f.read()dict_obj = json.loads(json_str)print(json.dumps(dict_obj, indent=2))------------------------------------------------------------"D:\Program Files\Python36\python3.exe" D:/MyProject/Python/workspace/test.py{ "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "pod-redis", "labels": { "name": "redis" } }, "spec": { "restartPolicy": "Always", "nodeSelector": { "zone": "node1" }, "containers": [ { "name": "pod-redis", "image": "docker.io/redis", "imagePullPolicy": "Never", "ports": [ { "containerPort": 6379, "hostPort": 8080 } ] } ] }}{ "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "pod-redis", "labels": { "name": "redis" } }, "spec": { "restartPolicy": "Always", "nodeSelector": { "zone": "node1" }, "containers": [ { "name": "pod-redis", "image": "docker.io/redis", "imagePullPolicy": "Never", "ports": [ { "containerPort": 6379, "hostPort": 8080 } ] } ] }}Process finished with exit code 0
转载地址:http://mcjqi.baihongyu.com/