简体   繁体   English

无法使用yaml方法从Java对象获取特定值

[英]Not able to get particular value from java object using yaml method

I am using yamlbeans to get data from yaml file. 我正在使用yamlbeans从yaml文件获取数据。 i am getting following response {x1=[{y1=z1}, {y2=z2}], x2=[{y1 =z1}, {y2=z2]} 我得到以下响应{x1 = [{y1 = z1},{y2 = z2}],x2 = [{y1 = z1},{y2 = z2]}

Now i want to get data y1 of x1 but i am not able to do this. 现在我想获取x1的数据y1,但我无法执行此操作。 I am using following code for read operation 我正在使用以下代码进行读取操作

package com.mobileapp;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.yamlbeans.YamlReader;

public class ReadDataWithYaml {

    public static void main(String[] args) { 

        try {

            YamlReader reader = new YamlReader(new FileReader("C:\\Users\\5521\\Desktop\\test.yml"));
            Object object = reader.read();
            System.out.println(object);
            Map<String, ArrayList<String>> map = (Map<String, ArrayList<String>>) object;
            System.out.println(map.get("x1"));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
    Yaml yaml1 = new Yaml();
    InputStream inputStream1 = Main.class.getClassLoader().getResourceAsStream("YourYaml.yaml");
    Map< String, Object> result = (Map< String, Object>) yaml1.load(inputStream1);
    for (Object name : result.keySet()) {
        System.out.println(result.get(name).toString());

    }

    String x= result.get("userInput").toString();
    System .out.println(""+x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM