Example usage for org.json.simple.parser JSONParser JSONParser

List of usage examples for org.json.simple.parser JSONParser JSONParser

Introduction

In this page you can find the example usage for org.json.simple.parser JSONParser JSONParser.

Prototype

JSONParser

Source Link

Usage

From source file:Connector.Embeddable.java

public void getConnection() {

    try {/*from w  w w.  j  ava 2s  .co m*/
        String envServices = System.getenv("VCAP_SERVICES");

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(envServices);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray vcapArray = (JSONArray) jsonObject.get("erservice");
        JSONObject vcap = (JSONObject) vcapArray.get(0);
        JSONObject credentials = (JSONObject) vcap.get("credentials");
        url = credentials.get("url").toString();
        password = credentials.get("password").toString();
        userid = credentials.get("userid").toString();

    } catch (ParseException ex) {
    }
}

From source file:Connector.Connector.java

private void setCredentials() {
    Map<String, String> env = System.getenv();

    if (env.containsKey("VCAP_SERVICES")) {

        try {/*from   w w  w  .ja  va 2s .c  o m*/
            JSONParser parser = new JSONParser();
            JSONObject vcap = (JSONObject) parser.parse(env.get("VCAP_SERVICES"));
            JSONObject service = null;

            for (Object key : vcap.keySet()) {
                String keyStr = (String) key;
                if (keyStr.toLowerCase().contains("tradeoff_analytics")) {
                    service = (JSONObject) ((JSONArray) vcap.get(keyStr)).get(0);
                    break;
                }
            }

            if (service != null) {
                JSONObject creds = (JSONObject) service.get("credentials");
                String username = (String) creds.get("username");
                String password = (String) creds.get("password");
                this.username = username;
                this.password = password;

                System.out.println(username);
                System.out.println(password);

            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

From source file:foldersync.Options.java

public Options(String config) throws IOException, ParseException {
    JSONParser parser = new JSONParser();
    JSONObject options = (JSONObject) parser.parse(new String(Files.readAllBytes(new File(config).toPath())));
    watches = (JSONArray) options.get("watches");
    source = (String) options.get("source");
    dest = (String) options.get("dest");
}

From source file:com.mycompany.craftdemo.utility.java

public static JSONObject getConfig(String filePath) {
    try {/*from w w w  .  ja  v a 2 s .  co m*/
        // read the json file
        FileReader reader = new FileReader(filePath);

        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

        return jsonObject;

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ParseException ex) {
        ex.printStackTrace();
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:alexaactions.SmartThingsTemperatureDevices.java

SmartThingsTemperatureDevices(SmartThingsAgent sa) {
    agent = sa;/* w ww. j ava 2  s . co m*/
    parser = new JSONParser();
    path = "temps";
    String jsonScale = agent.get(path + "/units");
    Object obj = null;
    JSONObject jobj = null;

    try {
        obj = (Object) parser.parse(jsonScale);
        jobj = (JSONObject) obj;
    } catch (ParseException ex) {
        Logger.getLogger(SmartThingsTemperatureDevices.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("defaulting to F scale");
        tempScale = "F";
    }
    tempScale = (String) jobj.get("units");
}

From source file:com.apigee.edge.config.utils.ConfigReader.java

/**
 * Example Hierarchy/*from   w ww  . ja  v  a2  s  .  co  m*/
 * envConfig.cache.<env>.caches
 * 
 * Returns List of
 * [ {cache1}, {cache2}, {cache3} ]
 */
public static List getEnvConfig(String env, File configFile) throws ParseException, IOException {

    Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    JSONParser parser = new JSONParser();
    ArrayList out = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile));

        JSONArray configs = (JSONArray) parser.parse(bufferedReader);

        if (configs == null)
            return null;

        out = new ArrayList();
        for (Object config : configs) {
            out.add(((JSONObject) config).toJSONString());
        }
    } catch (IOException ie) {
        logger.info(ie.getMessage());
        throw ie;
    } catch (ParseException pe) {
        logger.info(pe.getMessage());
        throw pe;
    }

    return out;
}

From source file:au.com.optus.pei.express.coef2.template.businessreq.BusinessRequestTemplate.java

public String brTemplate(String s) {
    StringBuilder sb = new StringBuilder();
    JSONParser parser = new JSONParser();
    try {//w ww.j  ava2s  . com
        Object obj = parser.parse(s);
        JSONArray array = (JSONArray) obj;

        Map<String, String> map = new HashMap<String, String>();
        Iterator<JSONObject> iterator = array.iterator();

        while (iterator.hasNext()) {
            JSONObject factObj = (JSONObject) iterator.next();
            map.put((String) factObj.get("name"), (String) factObj.get("value"));
        }

        sb.append("Dear EIES Engagement,");
        sb.append(
                "\nA new engagement request has been submitted via the online COEF Business Engagement Form. Details are as follows:");
        sb.append("\n\nRequest ID: 107");
        sb.append("\nName: ");
        sb.append(map.get("firstName"));
        sb.append("\nEmail:");
        sb.append(map.get("email"));
        sb.append("\nPhone: ");
        sb.append(map.get("phone"));
        sb.append("\nMobile:");
        sb.append(map.get("od[employeeMobile]"));
        sb.append("\nEmployee ID: ");
        sb.append(map.get("employeeID"));
        sb.append("\nWhat are you trying to achieve? ");
        sb.append(map.get("requestDescription"));
        if (map.containsKey("proposedStartDate")) {
            sb.append("\nWhen are you ready to start? ");
            sb.append(map.get("proposedStartDate"));
        }
        if (map.containsKey("proposedAvailableDate")) {
            sb.append("\nWhen would you like this change to be available? ");
            sb.append(map.get("proposedAvailableDate"));
        }

        sb.append("\nType of work requested: ");
        sb.append(map.get("requestType"));
        sb.append("\nCFU or Business Unit: ");
        sb.append(map.get("cfu"));
        sb.append("\nHas funding been identified for your request? ");
        sb.append(map.get("fundIdentified"));

        if (map.get("fundIdentified").equals("Yes")) {
            sb.append("\nWho will fund this? ");
            sb.append(map.get("sponsername"));
        } else {
            sb.append("\nDo you have a sponsor for this request? ");
            sb.append(map.get("haveSponser"));
        }
    } catch (ParseException pe) {
        // throws ;
    }

    return sb.toString();
}

From source file:cc_package.CC_Conf.java

public CC_Conf(String file) {

    JSONParser parser = new JSONParser();
    units = new ArrayList<>();
    names = new ArrayList<>();

    try {/*from   ww  w  .j a  v  a2  s  .  c om*/
        JSONObject jsonObject;
        InputStream is = this.getClass().getResourceAsStream("/" + file);
        jsonObject = (JSONObject) parser.parse(getStringFromInputStream(is));
        //jsonObject = (JSONObject) parser.parse(is);
        //Object obj = parser.parse(new FileReader("cc_test.json"));
        //JSONObject jsonObject = (JSONObject) obj;
        JSONArray junits = (JSONArray) jsonObject.get("units");
        Iterator<JSONObject> iterator = junits.iterator();

        while (iterator.hasNext()) {

            JSONObject all = iterator.next();
            String country = (String) all.get("CountryCurrency");
            String unit = (String) all.get("Units");
            units.add(unit);
            names.add(country);

        }
    } catch (Exception e) {

        e.printStackTrace();

    }
}

From source file:mysynopsis.JSONReader.java

public static void readData() throws FileNotFoundException, IOException, ParseException {

    try {/* w  ww.j av a  2  s. c o  m*/
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("data.json"));
        JSONObject read;
        read = (JSONObject) obj;

        name = (String) read.get("Name");
        initial = (String) read.get("Initial");
        biog = (String) read.get("Bio");
        designation = (String) read.get("Designation");
        dept = (String) read.get("Department");
        university = (String) read.get("University");
        universityAddress = (String) read.get("University_Address");
        office = (String) read.get("Office");
        officehours = (String) read.get("Ohours");
        phone = (String) read.get("Phone");
        email = (String) read.get("Email");
        resumelink = (String) read.get("Resume_Link");
        education = (String) read.get("Educational");
        professional = (String) read.get("Professional");
        awards = (String) read.get("Awards");

        imgExtension = (String) read.get("Image_Ext");
        imgString = (String) read.get("Image_String");

        /*JSONArray education = (JSONArray) read.get("Education");
                
        for(int i=0;i<8;i++) {
                
        educ[i] = (String) education.get(i);
                
        }*/
        JSONArray ftpinfo = (JSONArray) read.get("Server_Information");

        server = (String) ftpinfo.get(0);
        user = (String) ftpinfo.get(1);
        dir = (String) ftpinfo.get(2);
    } catch (IOException | ParseException e) {

        JOptionPane.showMessageDialog(null, "Something went wrong. Please re-run the software.");
    }
}

From source file:models.GeoModel.java

public String getCurrentAddress() {
    String json = GeoModel.getCurrentLocation();
    JSONParser parser = new JSONParser();

    try {/*w  ww  .  jav  a 2 s  .c o m*/
        // Console.WriteLn(json);
        JSONObject response = (JSONObject) parser.parse(json);

        if (!response.isEmpty()) {

            JSONObject content = (JSONObject) response.get("content");

            StringBuilder builder = new StringBuilder();

            String city = (String) content.get("city");
            String country = (String) content.get("country");
            String ip = (String) content.get("ip");
            String lat = (String) content.get("lat");
            String lng = (String) content.get("lng");
            builder.append((city == null || city == "null") ? "" : city);
            builder.append(",");
            builder.append((country == null || country == "null") ? "" : country);

            this.controller.xhsAsyncSearch(builder.toString(), 0);
            this.controller.xhsUpdateGeoPosition(builder.toString(), lat, lng);
            // Console.WriteLn("IP "+ip);
            return ip;

        }

    } catch (Exception err) {
        err.printStackTrace();

    }

    return null;
}