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:es.tid.keyserver.https.protocol.InputJSON.java

/**
 * Constructor using a String with the JSON definition.
 * @param stringData Contains the JSON as String.
 *///from  w w w  . java2s. co m
public InputJSON(String stringData) {
    inputData = new JSONObject();
    try {
        JSONParser parser = new JSONParser();
        inputData = (JSONObject) parser.parse(stringData);
    } catch (ParseException ex) {
        LOGGER.debug("Not valid input JSON: {}.", ex.toString());
    }
}

From source file:edu.pdx.konstan2.PortlandLive.StopsFactory.java

public Stop(String jsonString) {
    try {//  w ww  .  j  av a 2s. com
        JSONParser parser = new JSONParser();
        JSONObject jobj = (JSONObject) parser.parse(jsonString);
        jsonRepresntation = jobj;
        parse(jsonRepresntation);
    } catch (Exception e) {
    }

}

From source file:com.starr.smartbuilds.service.DataService.java

private void getItemsFromData(String data) throws IOException, ParseException {
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(data);
    JSONObject json_data = (JSONObject) json.get("data");
    Set<String> tags = new HashSet<String>();

    for (Object arr : json_data.values()) {
        JSONObject json_arr = (JSONObject) arr;
        Long id = (Long) json_arr.get("id");
        String description = (String) json_arr.get("description");
        String plaintext = (String) json_arr.get("plaintext");
        String name = (String) json_arr.get("name");
        JSONArray json_tags = (JSONArray) json_arr.get("tags");
        String tag_str = "";
        if (json_tags != null) {
            for (Object obj : json_tags) {
                String tag = (String) obj;
                if (!tag.equals("") && !tag.equals("")) {
                    tag_str += tag + "&";
                }/*w  w  w  .  j  a v a 2  s  .  co  m*/
                tags.add(tag);
            }
        }
        Item item = new Item();
        item.setId(id);
        item.setDescription(description);
        item.setName(name);
        item.setPlaintext(plaintext);
        item.setTag_str(tag_str);

        itemDAO.addItem(item);

    }

    for (String tag_name : tags) {
        Tag tag = new Tag();
        tag.setName(tag_name);
        tagDAO.addTag(tag);
    }
    saveManyToMany();
}

From source file:net.amigocraft.mpt.Main.java

@Override
public void onEnable() {
    plugin = this;
    log = this.getLogger();

    mainThreadId = Thread.currentThread().getId();

    saveDefaultConfig();//from  w  w  w .  j  a v  a 2s .  co m

    File rStoreFile = new File(getDataFolder(), "repositories.json");
    JSONParser parser = new JSONParser();
    if (rStoreFile.exists()) { // repo store has been initialized
        log.info("Loading local repository store...");
        try {
            repoStore = (JSONObject) parser.parse(new FileReader(rStoreFile));
        } catch (IOException | ParseException ex) {
            ex.printStackTrace();
            log.severe("Failed to load repository store!");
        }
    } else { // we need to initialize the repo store
        initializeRepoStore(rStoreFile);
    }

    File pStoreFile = new File(getDataFolder(), "packages.json");
    if (pStoreFile.exists()) { // package store has been initialized
        log.info("Loading local package store...");
        try {
            packageStore = ((JSONObject) parser.parse(new FileReader(pStoreFile)));
        } catch (IOException | ParseException ex) {
            ex.printStackTrace();
            log.severe("Failed to load package store!");
        }
    } else { // we need to initialize the package store
        initializePackageStore(pStoreFile);
    }

    this.getCommand("mpt").setExecutor(new CommandManager()); // register the CommandManager class

    // initialize auto-updater
    if (Config.AUTO_UPDATE)
        new Updater(this, 88254, this.getFile(), Updater.UpdateType.DEFAULT, true);

    // initialize plugin metrics
    if (Config.METRICS) {
        try {
            Metrics metrics = new Metrics(this);
            metrics.start();
        } catch (IOException ex) {
            log.warning("Failed to initialize plugin metrics!");
        }
    }

    log.info(this + " has been enabled!");
}

From source file:ClubImpl.java

public String getNews() throws IOException {
    JSONParser parser = new JSONParser();
    Object obj = null;/*from  w w  w .  j a va  2 s . c o m*/
    try {
        File f = new File(".");
        System.out.println(f.getAbsolutePath());
        File relative = new File("News.json");
        obj = parser.parse(new FileReader(relative));
    } catch (Exception ex) {
        Logger.getLogger(ClubImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    JSONObject jsonObject = (JSONObject) obj;
    return jsonObject.toJSONString();
}

From source file:com.cbagroup.sit.Transact.java

public String sendPOST() throws IOException, NoSuchAlgorithmException {

    String key = "api_key=cbatest123";
    //String url = "http://developer.cbagroup.com/api/CreditTransfer?api_key=cbatest123";
    String url = "http://developer.cbagroup.com/api/Transact?" + key;
    URL object = new URL(url);
    HttpURLConnection con = (HttpURLConnection) object.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    //con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept", "application/json");

    String urlParameters = "Country=Kenya&" + "TranType=Transact&" + "Reference=COMBAPI&" + "Currency=kes&"
            + "Account=1.2.2013&" + "Amount=10.25&" + "Narration=pay&" + "TransactionDate=1.2.2013&";

    // Send post request
    con.setDoOutput(true);//from w w  w  .java  2s. c  om
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }

    in.close();

    //print result
    System.out.println(response.toString());

    ////////// start  ////////////////////
    String result = response.toString();

    JSONParser parser = new JSONParser();

    try {

        Object obj = parser.parse(result);

        JSONObject jsonObject = (JSONObject) obj;
        //System.out.println(jsonObject);

        long ResCode = (long) jsonObject.get("Response Code");
        System.out.println();
        System.out.println("Response Code : " + ResCode);
        System.out.println();

        if (ResCode == 1) {

            System.out.println("#########################################################");
            System.out.println("Fred hack Fail");
            System.out.println();

            long ResCode1 = (long) jsonObject.get("Response Code");
            System.out.println();
            System.out.println("Response Code : " + ResCode1);
            System.out.println();

            String Ref = (String) jsonObject.get("Reference");
            System.out.println();
            System.out.println("Reference : " + Ref);
            System.out.println();

            String Des = (String) jsonObject.get("Description");
            System.out.println();
            System.out.println("Description : " + Des);
            System.out.println();

        } else {

            System.out.println("#########################################################");
            System.out.println("Fred hack Success");
            System.out.println();

            long ResCode1 = (long) jsonObject.get("Response Code");
            System.out.println();
            System.out.println("Response Code : " + ResCode1);
            System.out.println();

            String Ref = (String) jsonObject.get("Reference");
            System.out.println();
            System.out.println("Reference : " + Ref);
            System.out.println();

            String Des = (String) jsonObject.get("Description");
            System.out.println();
            System.out.println("Description : " + Des);
            System.out.println();

        }

        //            long age = (Long) jsonObject.get("Description");
        //            System.out.println(age);

        // loop array
        //            JSONArray msg = (JSONArray) jsonObject.get("messages");
        //            Iterator<String> iterator = msg.iterator();
        //            while (iterator.hasNext()) {
        //                System.out.println(iterator.next());
        //}

    } catch (ParseException e) {
        e.printStackTrace();
    }

    return response.toString();
}

From source file:eumetsat.pn.elasticsearch.webapp.ElasticsearchApp.java

@Override
protected Map<String, Object> describeProduct(String id) throws MalformedURLException, ParseException {
    Map<String, Object> data = new HashMap<>();

    //create the url with the id passed in argument
    URL url = new URL(this.productEndpointUrlString + id);

    HashMap<String, String> headers = new HashMap<>();
    HashMap<String, String> params = new HashMap<>();
    String body = null;// w  w w .j ava 2s  .  c om
    boolean debug = true;

    WebResponse response = rClient.doGetRequest(url, headers, params, body, debug);

    log.trace("response = " + response);

    JSONParser parser = new JSONParser();

    JSONObject jsObj = (JSONObject) parser.parse(response.body);

    Map<String, Object> identificationInfo = ((Map<String, Object>) (((Map<String, Object>) jsObj
            .get("_source")).get("identificationInfo")));

    data.put("id", id);
    data.put("title", identificationInfo.get("title"));
    if (identificationInfo.containsKey("keywords")) {
        data.put("abstract", identificationInfo.get("abstract"));
    }
    if (identificationInfo.containsKey("keywords")) {
        JSONArray keywords = (JSONArray) identificationInfo.get("keywords");
        data.put("keywords", Joiner.on(", ").join(keywords.iterator()));
    }
    if (identificationInfo.containsKey("thumbnail")) {
        data.put("thumbnail", identificationInfo.get("thumbnail").toString());
    }
    if (identificationInfo.containsKey("status")) {
        data.put("status", identificationInfo.get("status").toString());
    }
    if (identificationInfo.containsKey("satellite")) {
        data.put("satellite", identificationInfo.get("satellite").toString());
    }

    return data;
}

From source file:it.marcoberri.mbfasturl.helper.ConfigurationHelper.java

private ConfigurationHelper() {
    try {/*from www  .ja va  2  s  .  c  om*/
        final URL main = getClass().getProtectionDomain().getCodeSource().getLocation();
        final String path = URLDecoder.decode(main.getPath(), "utf-8");
        final String webInfFolderPosition = new File(path).getPath();
        final String webInfFolder = webInfFolderPosition.substring(0, webInfFolderPosition.indexOf("classes"));
        prop = new Properties();
        prop.load(FileUtils.openInputStream(new File(webInfFolder + File.separator + "config.properties")));

        final JSONParser parser = new JSONParser();

        final Object obj = parser.parse(new FileReader(webInfFolder + File.separator + "cron.json"));
        final JSONObject jsonObject = (JSONObject) obj;
        ConfigurationHelper.cron = (JSONArray) jsonObject.get("cron");

    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (ParseException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:de.phip1611.apps.numbered_filenames_with_leading_zeros.JSONConfigProvider.java

/**
 * Ldt die Konfigurationsdatei und parst die JSON-Daten in ein JSON-Objekt.
 * @throws FileNotFoundException /*  w  w  w.j  a v  a  2 s .  c o m*/
 */
public void loadConfig() throws FileNotFoundException {
    if (!this.isReadable()) {
        throw new FileNotFoundException("Konfig-Datei ist nicht vorhanden oder konnte nicht gelesen werden.");
    }
    try {
        config = (JSONObject) new JSONParser().parse(new FileReader(RELATIVE_CONFIGFILE_PATH));
    } catch (IOException | ParseException ex) {
        System.err.println("Konfig-Datei konnte nicht erfolgreich geffnet und geparst werden.");
    }
}

From source file:agileinterop.AgileInterop.java

/**
 * Example:/* w  w w.j a va  2  s.c om*/
 * {
 *     "startDate": "02/12/2014",
 *     "endDate": "03/01/2014",
 *     "database": "C:\\Users\\mburny\\Desktop\\AgileBot\\mt\\agile.db",
 *     "map": "C:\\Users\\mburny\\Desktop\\AgileBot\\mt\\map.json"
 * }
 * 
 * @param body
 * @return
 * @throws ParseException 
 */
private static JSONObject crawlAgileByDateOriginated(String body) throws ParseException {
    // Parse body as JSON
    JSONParser parser = new JSONParser();
    JSONObject jsonBody = (JSONObject) parser.parse(body);

    return null;
}