如何读取yaml中的列表?

时间:2011-03-22 23:25:03

标签: java list yaml

我正在尝试从yaml文件中读取输入,其中包含以下内容:

- !com.example.Contact
  name: Nathan Sweet
  age: 28
  address: pak
  phoneNumbers:
    - !com.example.Phone
      name: Home
      number: 1122
    - !com.example.Phone
      name: Work
      number: 3322
- !com.example.Contact
  name: Nathan Sw1eet
  age: 281
  address: pak1
  phoneNumbers:
    - !com.example.Phone
      name: Home1
      number: 11221
    - !com.example.Phone
      name: Work1
      number: 33211

我有以下定义:

import java.util.List;
import java.util.Map;

public class Contact 
{
    public String name;
    public int age;
    public String address;
    public List phoneNumbers;
}
public class Phone
{
    public String name;
    public String number;
}

有些人可以告诉我阅读这些电话号码的方法

3 个答案:

答案 0 :(得分:7)

我不得不重写你的yaml ......

- !!com.example.Contact
  name: Nathan Sweet
  age: 28
  address: pak
  phoneNumbers:
    - !!com.example.Phone
      name: Home
      number: 1122
    - !!com.example.Phone
      name: Work
      number: 3322
- !!com.example.Contact
  name: Nathan Sw1eet
  age: 281
  address: pak1
  phoneNumbers:
    - !!com.example.Phone
      name: Home1
      number: 11221
    - !!com.example.Phone
      name: Work1
      number: 33211

Java ...

package com.example;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

public class LoadContacts {
    private static String yamlLocation="classpath:contacts.yaml";

    public static void main(String[] args) throws IOException{
        Yaml yaml = new Yaml(new Constructor(Collection.class));
        InputStream in = null;
        Collection<Contact> contacts;
        try {
            in = new FileInputStream(new File(yamlLocation));
            contacts = (Collection<Contact>) yaml.load(in);
        } catch (IOException e) {
                final DefaultResourceLoader loader = new DefaultResourceLoader();
                final Resource resource = loader.getResource(yamlLocation);
                in = resource.getInputStream();
                contacts = (Collection<Contact>) yaml.load(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    // no-op
                }
            }
        }
        for(Contact contact:contacts){
            System.out.println(contact.name + ":" + contact.address + ":" + contact.age );
        }
    }
}

答案 1 :(得分:3)

您是否尝试过SnakeYaml

答案 2 :(得分:1)

由于我遇到这篇帖子来阅读用户对象列表,这是我使用Jackson

所做的
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class LoadContacts {

  private static String yamlLocation = "path_to/../contacts.yml";

  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

    try {
      List<Contact> contactList = mapper.readValue(new File(yamlLocation), new TypeReference<List<Contact>>(){});
      contactList.forEach(System.out::println);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

mapper.readValue(..)接受第一个参数的多个参数,例如URL,String。此解决方案使用File。 我为OP问题正确工作所做的一个改变是定义phoneNumbers如下:

   public List<Phone> phoneNumbers;