0

I know this is a quite basic question, but after reading some SO posts and google I couldn't understand the issue well, so.

I have a map object data, a System.out.println(data) shows me something like the following on the console:

{data=[{engine=engine1, keyword=keyword1, url=url1}, {engine=engine2, keyword=keyword2, url=url2}]}

Also, a data shows me LinkedHashMap@11 size=1 on the console.

Now I wanted to have the first "engine" key value of the first array of "data", aka engine1.

Before that I tried to obtain the first of the array, so I tried the following, but I get errors.

data.get("data")[1]
Cannot evaluate because of compilation error(s): The type of the expression must be an array type but it resolved to Object.

data.get("data").get(1)
Cannot evaluate because of compilation error(s): The method get(int) is undefined for the type Object.

How can I obtain the "engine1" value from "data"? Thanks for your help.

edit:

The code around it is as shown below. data is declared by Map<String, Object> data.

import org.yaml.snakeyaml.Yaml;

~~ 

InputStream inputStream = new FileInputStream(new File("student.yml"));

Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(inputStream);
System.out.println(data);

(not sure you need this info but just in case, "student.yml" file is as shown below)

data: 
  - engine: engine1
    keyword: keyword1
    url: url1
  - engine: engine2
    keyword: keyword2
    url: url2

1 Answer 1

1

Updating answer post question edit; Please find below, the solution you want;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class YamlParser {

    public static void main(String[] args) throws FileNotFoundException {

        InputStream inputStream = new FileInputStream(new File("src/main/resources/student.yml"));

        Yaml yaml = new Yaml();
        Map<String, Object> data = yaml.load(inputStream);
        System.out.println(data); // Prints full map
        List<Map<String,String>> eng = (List<Map<String,String>>) data.get("data"); //Casting object to List
        System.out.println(eng.get(0).get("engine")); // Prints the value : engine1
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. The map declaration is not by Map<String, List<Engine>> but as the added detail above on my question. Sorry for the lack of details since I'm new to Java and didn't think the info was needed.
So data.get("data").get(0) shows me data.get("data").get(0) Cannot evaluate because of compilation error(s): The method get(int) is undefined for the type Object.
Rewritten answer as per your needs, please find it.
Thanks for the rewrite. And boom it worked. Thanks
Nice answer. I'll just add that the simplest change is to just assign the result of yaml.load to a sufficiently defined type to begin with: Map<String, ArrayList<Map<String, String>>> data = yaml.load(inputStream);, which lets you do System.out.println(data.get("data").get(0).get("engine"));. Same thing really. Just pointing out that there's no need to break the logic into two phases. This avoids the need for a cast.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.