-1

I want to load a json gfile with java and store the data in variables

I've tried this but it always gives an error

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.*
...

        JsonParser parser = new JsonParser();
        try (FileReader reader = new FileReader("/opt/step-agent/conf/conf_internet_explorer.json"))
        {
            Object obj = JsonParser.parse(reader);
            JsonObject config = (JsonObject) obj;
            String driverPath = config.get("driverPath").getAsString();
            String HttpProxy = config.get("HttpProxy").getAsString();
            String SslProxy = config.get("SslProxy").getAsString();
            boolean proxy = (boolean) config.get("proxy");
        }catch (Exception e){
            e.printStackTrace();
        }

the json

{
    "config": {
        "driverPath":"path/to/driver",
        "proxy":true,
        "HttpProxy":"http://proxy.local:80",
        "SslProxy":"http://proxy.local:80",
    }
}

the error:

non-static method parse(java.io.Reader) cannot be referenced from a static context
Jocomol
  • 303
  • 6
  • 18

1 Answers1

1

You mixed up your var with the Class.

You should write

Object obj = parser.parse(reader);

parser is your var which contains a Instance of JsonParser which has the method parse()

instead of

Object obj = JsonParser.parse(reader);

JsonParser is the Class JsonParser which has no static method parse()

Bill F
  • 723
  • 11
  • 23
J. Doe
  • 88
  • 7