Java将yaml文件解析为json对象和字符串对象

1、引入依赖

snakeyaml:

<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.29</version>
</dependency>

json:

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>

common:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

2、加载yaml文件,并解析为json对象


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author liyinlong
 * @since 2021/11/16 10:03 上午
 */
public class YamlUtil {


    /**
     * 将yaml文件加载为标准json对象(加载后的对象类型是JSONObject不是LinkedHashMap)
     * @param filePath 文件路径
     * @return 标注json对象
     */
    private static JSONObject loadAsNormalJsonObject(String filePath) {
        Yaml yaml = new Yaml();
        Map jsonObject = yaml.loadAs(convertToString(filePath), Map.class);
        String jsonStr = JSONUtil.toJsonStr(jsonObject);
        JSONObject normalJsonObj = JSONObject.parseObject(jsonStr);
        return normalJsonObj;
    }

    public static void main(String[] args) throws IOException {
        String source = "/Users/liyinlong/elasticsearch/values.yaml";
        JSONObject srcObject = loadAsNormalJsonObject(source);

        System.out.println(srcObject);
    }

}

3、加载yaml文件,并解析为字符串

    public static String convertToString(String filePath) {
        String conf = null;
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(filePath));
            conf = IOUtils.toString(in, String.valueOf(StandardCharsets.UTF_8));
            System.out.println(conf);
        } catch (IOException e) {
            log.error("文件读取失败", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                log.error("文件关闭失败", e);
            }
        }
        return conf;
    }

可以使用以下步骤将YAML文件转换JSON和XML: 1. 添加以下依赖: ```xml <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.27</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.11.3</version> </dependency> ``` 2. 创建SnakeYAML解析器并使用它来读取YAML文件并将其转换Java对象: ```java import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { Yaml yaml = new Yaml(); File file = new File("person.yaml"); Person person = yaml.loadAs(file, Person.class); } } ``` 在这个例子中,我们使用SnakeYAML解析器来读取名为“person.yaml”的文件,并将其转换为Person对象。 3. 将Java对象转换JSON: ```java import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) throws IOException { Yaml yaml = new Yaml(); File file = new File("person.yaml"); Person person = yaml.loadAs(file, Person.class); ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(person); System.out.println(json); } } ``` 在这个例子中,我们使用Jackson库将Person对象转换JSON字符串。 4. 将Java对象转换为XML: ```java import com.fasterxml.jackson.dataformat.xml.XmlMapper; public class Main { public static void main(String[] args) throws IOException { Yaml yaml = new Yaml(); File file = new File("person.yaml"); Person person = yaml.loadAs(file, Person.class); XmlMapper xmlMapper = new XmlMapper(); String xml = xmlMapper.writeValueAsString(person); System.out.println(xml); } } ``` 在这个例子中,我们使用Jackson库将Person对象转换为XML字符串。 注意:在将YAML文件转换JSON和XML时,需要确保Java类的字段名称与YAML文件中的键名称相同。否则,可以使用@YamlProperty注释来指定Java类字段与YAML键之间的映射关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值