Search code examples
javaandroidyamlsnakeyaml

snakeYAML JAVA: using the object produced


I am attempting to use snakeYAML to load a YAML file into an object in an Android Java Class. How do I access the members of the resulting object?

Yaml yaml = new Yaml();
Object data = yaml.load(questionsStream);
Log.v(TAG2,data.toString());

The output is:

03-07 18:15:55.637: VERBOSE/Q_Engine Load Questions(615): [{Answer=Sun Jun 25 01:00:00 GMT+01:00 1950, ID=8, Meta Info={Main Topics=[Korean War]}, Obscurity=1, Question=When did the Korean War begin?}, etc...

I want to iterate through the list of Maps and access their members by their keywords.


Solution

  • Replace the second line with:

    List<Map<String, Object>> data = (List<Map<String, Object>>) yaml.load(questionsStream);
    

    The Objects from inside the Map can be cast to Maps or Lists and iterated through the same way, depending on the yaml file structure.