【问题标题】:Parse list of beans with snakeyaml使用snakeyaml 解析bean 列表
【发布时间】:2017-04-22 05:39:32
【问题描述】:

是否可以用snakeyaml解析以下内容并获得List<Radio>(其中Radio是合适的java bean)?

-
  id: chaine416
  name: 'France Inter'
  type: music
-
  id: chaine417
  name: 'France Culture'
  type: music
-
  id: chaine418
  name: 'Couleur 3'
  type: music

new Yaml().load(...); 返回一个 List<HashMap>,但我想得到一个 List<Radio>

【问题讨论】:

    标签: java yaml snakeyaml


    【解决方案1】:

    我知道的唯一方法是使用顶部对象来处理集合。

    Yaml 文件:

    ---
    stations:
    -
      id: chaine416
      name: "France Inter"
      type: music
    -
      id: chaine417
      name: "France Culture"
      type: music
    -
      id: chaine418
      name: "Couleur 3"
      type: music
    

    我刚刚添加了 "---" ,新文档和属性 stations

    然后:

    package snakeyaml;
    
    import java.util.ArrayList;
    
    public class Radios {
    
        ArrayList<RadioStation> stations = new ArrayList<RadioStation>();
    
        public ArrayList<RadioStation> getStations() {
            return stations;
        }
    
        public void setStations(ArrayList<RadioStation> stations) {
            this.stations = stations;
        }
    }
    

    RadioStation 类:

    package snakeyaml;
    
    
    public class RadioStation {
        String id;
        String name;
        String type;
    
    
        public RadioStation(){
    
        }
    
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
    
        @Override
        public String toString() {
            return "RadioStation{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", type='" + type + '\'' +
                    '}';
        }
    }
    

    并读取 YAML 文件:

    package snakeyaml;
    
    import org.yaml.snakeyaml.Yaml;
    import org.yaml.snakeyaml.constructor.Constructor;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    
    public class Test {
    
        public static void main(String[] args) {
            Yaml yaml = new Yaml(new Constructor(Radios.class));
            try {
               Radios  result = (Radios) yaml.load(new FileInputStream("/home/ofe/dev/projets/projets_non_byo/TachesInfoengine/src/snakeyaml/data.yaml"));
                for (RadioStation radioStation : result.getStations()) {
                    System.out.println("radioStation = " + radioStation);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    【讨论】:

    • 这需要更改yaml格式,因为它是由第三方Web服务提供的,我无法做到。
    • 你只需要在开头插入两行:
    • 嗯,这闻起来像个黑客......但不管用什么都行 :) 谢谢
    【解决方案2】:

    实际上,Yaml 中有一个方法 loadAll,它返回 Iterable,但 yaml 文件在列表项之间应该有一个分隔符“---”。 该文件应如下所示:

    id: chaine416
    name: 'Fr
    ance Inter'
    type: music
    ---
    id: chaine417
    name: 'France Culture'
    type: music
    ---
    id: chaine418
    name: 'Couleur 3'
    type: music
    

    (省略了 Radio DTO)代码应该是这样的:

    public class Test {
    
        public static void main(String[] args) {
            Yaml yaml = new Yaml(new Constructor(Radio.class));
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("yourFile.yaml");
            
            List<Radio> radios = StreamSupport.stream(yaml.loadAll(inputStream).spliterator(), false)
                    .filter(Radio.class::isInstance)
                    .map(Radio.class::cast)
                    .collect(Collectors.toList());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 2015-08-31
      • 1970-01-01
      • 2019-12-11
      • 2017-04-04
      • 2011-02-12
      • 2020-03-12
      • 2020-03-21
      • 1970-01-01
      相关资源
      最近更新 更多