내가 아는 유일한 방법은 컬렉션을 처리하는 최상위 개체를 사용하는 것입니다.
YAML 파일 :
---
stations:
-
id: chaine416
name: "France Inter"
type: music
-
id: chaine417
name: "France Culture"
type: music
-
id: chaine418
name: "Couleur 3"
type: music
난 그냥, "---"새 문서 및 속성 스테이션을 추가했다. 그런 다음
:
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 파일을 읽을 수있는 다음 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();
}
}
}
을 제 3 자 웹 서비스에 의해 제공되기 때문에 실제로 그렇게합니다. – fiddler
글쎄, 시작 부분에 2 줄을 삽입하면된다. – olikaf
글쎄, 해킹 같은 냄새가 난다. – fiddler