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:net.bashtech.geobot.JSONUtil.java

public static String BOISeed(String channel) {

    try {/*from  ww w . j ava 2  s. com*/
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(
                BotManager.getRemoteContent("http://coebot.tv/configs/boir/" + channel.substring(1) + ".json"));

        JSONObject jsonObject = (JSONObject) obj;

        String seed = (String) jsonObject.get("seed");

        return seed;
    } catch (Exception ex) {
        return null;
    }
}

From source file:de.hstsoft.sdeep.NoteManager.java

public void loadNotes() throws IOException, ParseException {

    File file = new File(directory + FILENAME);
    if (file.exists()) {

        Reader reader = new BufferedReader(new FileReader(file));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        reader.close();/*from  w  ww  .  j a  va2 s. co m*/

        int version = Integer.parseInt(json.get("version").toString());
        if (VERSION != version) {
            System.out.println("Can not load Notes. invalid version.");
        }

        JSONArray notesArray = (JSONArray) json.get("notes");

        this.notes = new ArrayList<>();
        @SuppressWarnings("rawtypes")
        Iterator iterator = notesArray.iterator();
        while (iterator.hasNext()) {
            JSONObject noteJson = (JSONObject) iterator.next();
            Note note = Note.fromJson(noteJson);
            notes.add(note);
        }
    }

}

From source file:com.mstiles92.plugins.bookrules.localization.LocalizationHandler.java

/**
 * Load the selected language from the language file stored within the jar.
 *
 * @param language the language to load/*from  w  ww  .ja v  a2 s . co m*/
 * @return true if loading succeeded, false if it failed
 */
public boolean loadLocalization(Language language) {
    String contents;

    InputStream in = LocalizationHandler.class.getResourceAsStream(language.getPath());
    try {
        contents = CharStreams.toString(new InputStreamReader(in, "UTF-8"));
    } catch (IOException e) {
        return false;
    }

    JSONParser parser = new JSONParser();
    Object obj = null;

    try {
        obj = parser.parse(contents);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    if (obj == null) {
        return false;
    } else {
        this.jsonObject = (JSONObject) obj;
        return true;
    }
}

From source file:com.boundlessgeo.geoserver.json.JSONWrapper.java

/**
 * Decodes JSON content returning a wrapper.
 *
 * @param input Input JSON./*  w  w w  .  j  a  va 2 s  . co  m*/
 *
 * @return The wrapper.
 */
public static JSONWrapper<?> read(Reader input) throws IOException {
    try {
        return wrap(new JSONParser().parse(input));
    } catch (ParseException e) {
        throw new IOException("Parsing error", e);
    }
}

From source file:fr.treeptik.cloudunit.docker.DockerContainerJSON.java

private static JSONObject parser(String message) throws ParseException {
    JSONParser jsonParser = new JSONParser();
    JSONObject json = (JSONObject) jsonParser.parse(message);
    return json;/*from w  w  w  . j av  a 2  s .  c om*/
}

From source file:com.grantedbyme.example.ServletAjax.java

public void doProcess(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json; charset=UTF-8");

    String operation = request.getParameter("operation");
    int challengeType = Integer.valueOf(request.getParameter("challenge_type"));
    Boolean isSuccess = false;/*w ww . java2 s. co  m*/
    JSONObject result = null;
    JSONObject defaultResult = null;
    try {
        defaultResult = (JSONObject) new JSONParser().parse("{\"success\": false, \"error\": 0}");
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (operation != null) {
        _log(operation);
        GrantedByMe sdk = ServletUtils.getSDK(this);
        //sdk.setDebugMode(true);
        // process operation
        String challenge = null;
        if (operation.equals("getChallenge")) {
            result = sdk.getChallenge(challengeType);
        } else if (operation.equals("getChallengeState")) {
            challenge = request.getParameter("challenge");
            result = sdk.getChallengeState(challenge);
            if (challengeType == GrantedByMe.CHALLENGE_AUTHORIZE) {
                handleGetAccountState(result, challenge, sdk);
            } else if (challengeType == GrantedByMe.CHALLENGE_AUTHENTICATE) {
                handleGetSessionState(result);
            } else if (challengeType == GrantedByMe.CHALLENGE_PROFILE) {
                handleGetRegisterState(result, challenge, sdk);
            }
        }
    }
    if (result == null) {
        result = defaultResult;
    }

    PrintWriter out = response.getWriter();
    out.print(result.toJSONString());
    out.close();
}

From source file:com.criticalsoftware.mobics.presentation.util.GeolocationUtil.java

/**
 * Get the full address name/*from  w w  w  . j  a v a2s  . c  o  m*/
 *
 * @param latitude
 * @param longitude
 * @return address string
 */
public static String getAddressFromCoordinates(String latitude, String longitude) {

    String address = null;

    String url = MessageFormat.format(GEOLOCATION_URL, Configuration.INSTANCE.getGeolocationServer(), latitude,
            longitude);

    HttpMethod method = new GetMethod(url);

    try {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Making reverse geolocation call to: {}", url);
        }
        int statusCode = new HttpClient().executeMethod(method);

        if (statusCode == HttpStatus.SC_OK) {
            byte[] responseBody = readResponse(method);
            JSONObject jsonObject = (JSONObject) new JSONParser().parse(new String(responseBody));
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(jsonObject.toJSONString());
            }
            address = getFormattedAddress(jsonObject);
        } else {
            LOGGER.warn("Recived a HTTP status {}. Response was not good from {}", statusCode, url);
        }
    } catch (HttpException e) {
        LOGGER.error("Error while making call.", e);
    } catch (IOException e) {
        LOGGER.error("Error while reading the response.", e);
    } catch (ParseException e) {
        LOGGER.error("Error while parsing json response.", e);
    }

    return address;
}

From source file:jenkins.plugins.testrail.util.TestRailJsonParser.java

/**
 *
 * @param oldPlanJson/*from   w  w w.j av a 2s . c om*/
 * @param oldPlanTestsJson
 * @return
 */
public String createNewPlan(String oldPlanJson, Map<String, String> oldPlanTestsJson) throws ParseException {
    LOGGER.debug("createNewPlan() called.");
    LOGGER.debug(oldPlanJson);
    final JSONObject oldRootJsonObject = (JSONObject) new JSONParser().parse(oldPlanJson);

    LOGGER.debug("Grabbing first JSON entries");
    final String testName = oldRootJsonObject.get("name").toString();
    final String testDescription = oldRootJsonObject.get("description").toString();
    final String testMilestoneId = oldRootJsonObject.get("milestone_id").toString();
    LOGGER.debug("testName: " + testName);
    LOGGER.debug("testDescription: " + testDescription);
    LOGGER.debug("testMilestoneId: " + testMilestoneId);
    this.newTestPlanJsonObject.put("name", testName);
    this.newTestPlanJsonObject.put("description", testDescription);
    this.newTestPlanJsonObject.put("milestone_id", testMilestoneId);

    // Create new "entries" array and populate it
    LOGGER.debug("Parsing entries...");
    JSONArray newEntriesArray = new JSONArray();
    JSONArray oldEntriesArray = (JSONArray) oldRootJsonObject.get("entries");
    for (Object oldEntryObject : oldEntriesArray) {
        JSONObject oldEntryJsonObject = (JSONObject) oldEntryObject;
        JSONObject newEntryJsonObject = new JSONObject();

        newEntryJsonObject.put("suite_id", oldEntryJsonObject.get("suite_id").toString());
        newEntryJsonObject.put("include_all", false);

        JSONArray newCaseIdsArray = new JSONArray();
        JSONArray newConfigIdsArray = new JSONArray();

        JSONArray oldRunsJsonArray = (JSONArray) oldEntryJsonObject.get("runs");
        JSONArray newRunsJsonArray = new JSONArray();
        for (Object oldRunObj : oldRunsJsonArray) {
            JSONObject oldRunJsonObj = (JSONObject) oldRunObj;
            JSONObject newRunJsonObj = new JSONObject();
            JSONArray newRunCaseIdsArray = new JSONArray();
            JSONArray newRunConfigIdsArray = new JSONArray();
            //newCaseIdsArray.add(((JSONObject) run_obj).get("id"));
            String testId = oldRunJsonObj.get("id").toString();
            JSONArray testJsonArray = (JSONArray) new JSONParser().parse(oldPlanTestsJson.get(testId));
            for (Object testObject : testJsonArray) {
                JSONObject testJsonObject = (JSONObject) testObject;
                newCaseIdsArray.add(testJsonObject.get("case_id"));
                newRunCaseIdsArray.add(testJsonObject.get("case_id"));
            }
            JSONArray oldConfigIdsJsonArray = (JSONArray) oldRunJsonObj.get("config_ids");
            for (Object configIdObject : oldConfigIdsJsonArray) {
                final String configIdString = configIdObject.toString();
                newConfigIdsArray.add(configIdString);
                newRunConfigIdsArray.add(configIdString);
            }
            newRunJsonObj.put("include_all", false);
            newRunJsonObj.put("assignedto_id", null);
            newRunJsonObj.put("case_ids", newRunCaseIdsArray);
            newRunJsonObj.put("config_ids", newRunConfigIdsArray);

            newRunsJsonArray.add(newRunJsonObj);
        }
        newEntryJsonObject.put("case_ids", newCaseIdsArray);
        newEntryJsonObject.put("config_ids", newConfigIdsArray);
        newEntryJsonObject.put("runs", newRunsJsonArray);

        newEntriesArray.add(newEntryJsonObject);

    }
    // Insert new "entries" array
    this.newTestPlanJsonObject.put("entries", newEntriesArray);

    this.newTestPlanJsonString = this.newTestPlanJsonObject.toJSONString();

    return this.newTestPlanJsonString;
}

From source file:com.serena.rlc.provider.jira.domain.Issue.java

public static Issue parseSingle(String options) {
    JSONParser parser = new JSONParser();
    try {//from w w  w. j  a va  2 s .  c  o m
        Object parsedObject = parser.parse(options);
        JSONObject jsonObject = (JSONObject) parsedObject;
        Issue issue = parseSingle(jsonObject);
        return issue;
    } catch (ParseException e) {
        logger.error("Error while parsing input JSON - " + options, e);
    }
    return null;
}

From source file:com.valygard.aohruthless.utils.config.JsonConfiguration.java

/**
 * Constructor initializes a new JSON file by a given directory and file
 * name. If the directory does not exist, a new one is created. If the file
 * name does not end with the .json marker, it is properly appended.
 * //from w w w . j a  va 2s .c  om
 * @param dir
 *            the Directory path
 * @param fileName
 *            the Json file name
 */
public JsonConfiguration(File dir, String fileName) {
    if (!fileName.endsWith(".json")) {
        fileName += ".json";
    }
    this.fileName = fileName;

    dir.mkdirs();
    this.file = new File(dir, fileName);
    this.parser = new JSONParser();
    this.gson = new GsonBuilder().setPrettyPrinting().create();

    try {
        // hacky operation to determine if file is blank
        if (file.createNewFile() || file.length() <= 0) {
            this.obj = new JSONObject();
        } else {
            this.obj = (JSONObject) parser.parse(initReader());
        }
    } catch (IOException | ParseException e) {
        Bukkit.getLogger().severe("Could not parse JSON file '" + fileName + "'!");
        this.obj = new JSONObject();
    }
    initReader();
}