importyamlimportjson# 假设这是你的YAML数据yaml_data="""person: name: John Doe age: 30 married: true children: - name: Jane Doe age: 10 - name: Baby Doe age: 2"""# 将YAML转换为Python字典data=yaml.safe_load(yaml_data)# 将Python字典转换为JSON字符串json_data=json.dumps(data,indent=4)print(json_data)
constyaml=require('js-yaml');// 假设这是你的YAML数据constyamlData=`person: name: John Doe age: 30 married: true children: - name: Jane Doe age: 10 - name: Baby Doe age: 2`;// 将YAML转换为JavaScript对象constdata=yaml.load(yamlData);// 将JavaScript对象转换为JSON字符串constjsonData=JSON.stringify(data,null,2);console.log(jsonData);