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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With