Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML Beans reading nested objects

Tags:

java

yaml

mongodb

I have a YAML file in this format:

MongoDB:
       - server:
          - host: localhost:27017
          - minOplogHours: 100
          - maxSecondaryDelay: 120
       - server:
          - host: localhost:27018
          - minOplogHours: 100
          - maxSecondaryDelay: 120
       - server:
          - host: localhost:27019
          - minOplogHours: 100
          - maxSecondaryDelay: 120

I need to be able to access the data from each of the host: fields for each server: how can I achieve this?

so far I have this:

  YamlReader reader = new YamlReader(new FileReader("src/mongodb.yml"));
  Object object = reader.read();

  Map resultMap = (Map) object;
  System.out.println(resultMap.get("MongoDB"));

which prints:

[{server=[{host=localhost:27017}, {minOplogHours=100}, {maxSecondaryDelay=120}]}, {server=[{host=localhost:27018}, {minOplogHours=100}, {maxSecondaryDelay=120}]}, {server=[{host=localhost:27019}, {minOplogHours=100}, {maxSecondaryDelay=120}]}]

how can extract the server details from this string? more specifically how can i get host: values.

like image 830
Chris Edwards Avatar asked Aug 08 '26 02:08

Chris Edwards


1 Answers

I know its an old post but I don't see any response. Here is what I think will work for you.

Replace

Map resultMap = (Map) object;

with

Map<String, Map<String, Map<String, String>>> resultMap = (Map<String, Map<String, Map<String, String>>>) object;

I have had something like this and it worked. I hope this might help someone.

like image 106
pam kaur Avatar answered Aug 10 '26 17:08

pam kaur