java 如何阅读yaml中的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5399100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read lists in yaml?
提问by PLO
I am trying to read inputs from a yaml file, having the following content:
我正在尝试从 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
I have the following defined:
我有以下定义:
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;
}
Can some tell me the way to read these phone numbers
有人能告诉我如何读取这些电话号码吗
回答by qwerty
I had to rewrite your yaml a bit ...
我不得不稍微重写你的 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 );
}
}
}
回答by Neo
Since I came across this post to read a list of User objects, here is what I did using Hymanson
由于我遇到这篇文章是为了阅读用户对象列表,因此我使用的是 Hymanson
import com.fasterxml.Hymanson.core.type.TypeReference;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.fasterxml.Hymanson.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(..)
takes in multiple arguments such as URL, String for the first param. This solution uses File
.
One change I did for the OP problem to work correctly was to define phoneNumbers as follows:
mapper.readValue(..)
接受多个参数,例如 URL,第一个参数的字符串。此解决方案使用File
. 我为使 OP 问题正常工作所做的一项更改是将 phoneNumbers 定义如下:
public List<Phone> phoneNumbers;