私が知っている唯一の方法は、トップオブジェクトを使用してコレクションを処理することです。私はちょうど追加
---
stations:
-
id: chaine416
name: "France Inter"
type: music
-
id: chaine417
name: "France Culture"
type: music
-
id: chaine418
name: "Couleur 3"
type: music
"---"、新しい文書および属性ステーション:
YAMLファイル。その後
:
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サービスによって提供されているので – fiddler
さて、初めに2行挿入する必要があります: – olikaf
これは、ハックのような臭いです...しかし、何が動作します:)ありがとう – fiddler