<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.6.5</version>
</dependency>
//读取 JSON 字符串
JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonStr);
//转换成 YAML 字符串
String yamlStr = new YAMLMapper().writeValueAsString(jsonNodeTree);
---
apiVersion: "app.alauda.io/v1alpha1"
kind: "HelmRequest"
metadata:
name: "mongodb-java-6666"
spec:
chart: "bitnami/mongodb"
namespace: "test-hl2"
releaseName: "mongodb-java-6666"
values:
service:
type: "NodePort"
auth:
rootUser: "admin"
rootPassword: "admin123123"
version: "11.0.4"
该方式会自动带上 ---
分隔符,大部分属性值也默认带上了双引号,若不想要引号,可使用下面的方式
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.18</version>
</dependency>
Yaml yaml = new Yaml();
//将 JSON 字符串转成 Map
Map<String,Object> map = (Map<String, Object>) yaml.load(jsonUrl);
//转换成 YAML 字符串
String yamlStr = yaml.dumpAsMap(map);
kind: Service
apiVersion: v1
metadata:
name: tomcat
labels:
app: tomcat
spec:
ports:
- name: tcp-8080
protocol: TCP
port: 8080
selector:
app: tomcat
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/123252039
内容来源于网络,如有侵权,请联系作者删除!