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

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

Introduction

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

Prototype

public Object parse(Reader in) throws IOException, ParseException 

Source Link

Usage

From source file:msuresh.raftdistdb.RaftCluster.java

public static void InitPortNumber() {
    try {//from   w w w.j  a  v a 2  s  .c o m
        File f = new File(Constants.STATE_LOCATION + "global.info");
        if (!f.exists()) {
            createDefaultGlobal();
        }
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader(Constants.STATE_LOCATION + "global.info"));
        JSONObject jsonObject = (JSONObject) obj;
        Long a = (Long) jsonObject.get("currentCount");
        portId = a.intValue();
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:iracing.webapi.WorldRecordsParser.java

static WorldRecords parse(String json, boolean includeApiUserRow) {
    JSONParser parser = new JSONParser();
    WorldRecords output = null;//w  w w . java 2s  .  c  o m
    try {
        JSONObject root = (JSONObject) parser.parse(json);
        output = new WorldRecords();
        JSONObject root2 = (JSONObject) root.get("d");
        output.setApiUserRow(getLong(root2, "22"));
        output.setTotalResults(getLong(root2, "32"));
        JSONArray rootArray = (JSONArray) root2.get("r");
        List<WorldRecord> recordList = new ArrayList<WorldRecord>();
        for (int i = 0; i < rootArray.size(); i++) {
            JSONObject r = (JSONObject) rootArray.get(i);
            long recordNumber = getLong(r, "36");
            if (!includeApiUserRow && recordNumber == 0)
                continue;
            WorldRecord wr = new WorldRecord();
            wr.setClubName(getString(r, "1", true));
            wr.setCountryCode(getString(r, "2"));
            wr.setLicenseSubLevel(getInt(r, "3"));
            wr.setIrating(getInt(r, "4"));
            wr.setTimeTrialSubSessionId(getLong(r, "5"));
            wr.setQualify(getString(r, "6", true));
            IracingCustomer driver = new IracingCustomer();
            driver.setId(getLong(r, "26"));
            driver.setName(getString(r, "7", true));
            wr.setDriver(driver);
            long l = getLong(r, "8");
            if (l > 0)
                wr.setPracticeStartTime(new Date(l));
            wr.setSeasonQuarter(getInt(r, "9"));
            wr.setClubId(getInt(r, "10"));
            wr.setRace(getString(r, "11", true));
            wr.setLicenseSubLevelText(getString(r, "13", true));
            wr.setSeasonYear(getInt(r, "14"));
            wr.setTimeTrialRating(getInt(r, "15"));
            wr.setLicenseGroupId(getInt(r, "16"));
            wr.setRaceSubSessionId(getLong(r, "17"));
            l = getLong(r, "18");
            if (l > 0)
                wr.setTimeTrialStartTime(new Date(l));
            wr.setQualifyingSubSessionId(getLong(r, "19"));
            wr.setLicenseLevelId(getInt(r, "20"));
            wr.setTrackId(getInt(r, "21"));
            wr.setTimeTrial(getString(r, "23", true));
            wr.setCarId(getInt(r, "28"));
            wr.setCategoryId(getInt(r, "29"));
            wr.setRegionName(getString(r, "30", true));
            wr.setPracticeSubSessionId(getLong(r, "31"));
            l = getLong(r, "33");
            if (l > 0)
                wr.setRaceStartTime(new Date(l));
            wr.setPractice(getString(r, "34", true));
            l = getLong(r, "35");
            wr.setRecordNumber(recordNumber);
            if (l > 0)
                wr.setQualifyingStartTime(new Date(l));
            wr.setCategoryName(getString(r, "37"));
            recordList.add(wr);
        }
        output.setRecords(recordList);
    } catch (ParseException ex) {
        Logger.getLogger(WorldRecordsParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.ecofactor.qa.automation.util.JsonUtil.java

/**
 * Parses the array.// w w w . j a  v a  2 s .  c o  m
 * @param content the content
 * @return the jSON array
 */
public static JSONArray parseArray(final String content) {

    final JSONParser parser = new JSONParser();
    try {
        return (JSONArray) parser.parse(content);
    } catch (ParseException e) {
        LOGGER.error(e.getMessage());
    }
    return null;
}

From source file:com.mtgox.examples.UsageExample.java

public static ApiKeys readApiKeys(String pathToJsonFile) {
    //see https://code.google.com/p/json-simple/wiki/DecodingExamples
    JSONParser parser = new JSONParser();
    ApiKeys apiKeys = null;//w ww .j  a va  2  s  . c om
    String apiStr = Utils.readFromFile(pathToJsonFile);
    try {
        JSONObject obj2 = (JSONObject) (parser.parse(apiStr));
        apiKeys = new ApiKeys((String) obj2.get("mtgox_secret_key"), (String) obj2.get("mtgox_api_key"));
    } catch (ParseException ex) {
        System.err.println(ex);
    }
    return apiKeys;
}

From source file:com.ecofactor.qa.automation.util.JsonUtil.java

/**
 * Parses the object./*from   ww  w  .  ja  v a  2 s  .  c  o  m*/
 * @param content the content
 * @return the jSON object
 */
public static JSONObject parseObject(final String content) {

    final JSONParser parser = new JSONParser();
    try {
        return (JSONObject) parser.parse(content);
    } catch (ParseException e) {
        LOGGER.error(e.getMessage());
    }
    return null;
}

From source file:models.Snack.java

public static ArrayList getsnackList() throws IOException {
    ArrayList<Snack> snackList = new ArrayList();

    try {/*  ww w  .  j  ava  2 s  .c  om*/

        InputStream input = new URL(url).openStream();
        String genreJson = IOUtils.toString(input);

        JSONParser parser = new JSONParser();

        JSONArray tileJson = (JSONArray) parser.parse(genreJson);

        for (Object object : tileJson) {
            JSONObject aJson = (JSONObject) object;

            long id = (Long) aJson.get("id");
            String name = (String) aJson.get("name");
            boolean opt = (Boolean) aJson.get("optional");
            String loc = (String) aJson.get("purchaseLocations");
            long count = (Long) aJson.get("purchaseCount");
            String date = (String) aJson.get("lastPurchaseDate");
            JSONArray imgSize = (JSONArray) aJson.get("size");
            snackList.add(new Snack(id, name, opt, loc, count, date));

        }

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

    return snackList;
}

From source file:com.wso2.raspberrypi.apicalls.APICall.java

/**
 * Populates Token object using folloing JSON String
 * {//from w  ww.  j a v  a 2 s .c  o  m
 * "token_type": "bearer",
 * "expires_in": 3600000,
 * "refresh_token": "f43de118a489d56c3b3b7ba77a1549e",
 * "access_token": "269becaec9b8b292906b3f9e69b5a9"
 * }
 *
 * @param accessTokenJson
 * @return
 */
public static Token getAccessToken(String accessTokenJson) {
    JSONParser parser = new JSONParser();
    Token token = new Token();
    try {
        Object obj = parser.parse(accessTokenJson);
        JSONObject jsonObject = (JSONObject) obj;
        token.setAccessToken((String) jsonObject.get("access_token"));
        long expiresIn = ((Long) jsonObject.get("expires_in")).intValue();
        token.setExpiresIn(expiresIn);
        token.setRefreshToken((String) jsonObject.get("refresh_token"));
        token.setTokenType((String) jsonObject.get("token_type"));

    } catch (ParseException e) {
        log.error("", e);
    }
    return token;

}

From source file:com.server.GCS.java

public static CloudObject find(String bucket, String key) {
    try {/*  ww  w  . j  av  a  2s.c  o m*/
        Storage.Objects.Get getObject = storage.objects().get(bucket, key);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        getObject.executeMediaAndDownloadTo(out);
        String value = out.toString();
        JSONObject obj;
        JSONParser p = new JSONParser();
        obj = (JSONObject) p.parse(value);

        return new CloudObject(getObject.getObject(), obj);

    } catch (com.google.api.client.http.HttpResponseException e) {
        //         Log.e(tag, "Exception: " + e.getMessage());
        e.printStackTrace();
        return null;
    } catch (Exception e) {
        //         Log.e(tag, "Exception: " + e.printStackTrace());
        e.printStackTrace();
        return null;
    }
}

From source file:com.stratio.deep.aerospike.AerospikeJavaRDDFT.java

/**
 * Imports dataset./*from  w w w.  j  a v  a2s .  c o m*/
 *
 * @throws java.io.IOException
 */
private static void dataSetImport() throws IOException, ParseException {
    URL url = Resources.getResource(DATA_SET_NAME);
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(new FileReader(url.getFile()));
    JSONObject jsonObject = (JSONObject) obj;

    String id = (String) jsonObject.get("id");
    JSONObject metadata = (JSONObject) jsonObject.get("metadata");
    JSONArray cantos = (JSONArray) jsonObject.get("cantos");

    Key key = new Key(NAMESPACE_ENTITY, SET_NAME, id);
    Bin binId = new Bin("id", id);
    Bin binMetadata = new Bin("metadata", metadata);
    Bin binCantos = new Bin("cantos", cantos);
    aerospike.put(null, key, binId, binMetadata, binCantos);
    aerospike.createIndex(null, NAMESPACE_ENTITY, SET_NAME, "id_idx", "id", IndexType.STRING);

    Key key2 = new Key(NAMESPACE_CELL, SET_NAME, 3);
    Bin bin_id = new Bin("_id", "3");
    Bin bin_number = new Bin("number", 3);
    Bin bin_text = new Bin("message", "new message test");
    aerospike.put(null, key2, bin_id, bin_number, bin_text);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "num_idx", "number", IndexType.NUMERIC);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "_id_idx", "_id", IndexType.STRING);
}

From source file:ch.uzh.phys.ecn.oboma.map.api.MapFactory.java

public static INodeMap buildDefaultSBBMap() throws Exception {
    NodeMap map = new NodeMap();
    Random rand = new Random();
    int stationCount = 0;
    int trainCount = 0;

    JSONParser parser = new JSONParser();

    try {// w ww . j a v a 2s.c  om
        JSONArray stations = (JSONArray) parser.parse(Files.newBufferedReader(Paths.get("data/stations.json")));
        for (Object o : stations) {
            JSONObject station = (JSONObject) o;

            String id = (String) station.get("id");
            String name = (String) station.get("name");
            JSONObject coord = (JSONObject) ((JSONObject) station.get("location")).get("wgs");
            Double lat = (Double) coord.get("y");
            Double lng = (Double) coord.get("x");

            Node node = new Node(id, name, lat, lng, 0, 0);
            map.add(node);
            stationCount++;

            LOGGER.info(String.format("Station Node %s (%s) added", id, name));
        }

        JSONArray connections = (JSONArray) parser
                .parse(Files.newBufferedReader(Paths.get("data/connections.json")));
        for (Object o : connections) {
            JSONObject connection = (JSONObject) o;

            String from = (String) connection.get("from");
            String to = (String) connection.get("to");
            int time = (int) ((long) connection.get("time"));

            String id1 = String.format("%s-%s", from, to);
            String name1 = String.format("Train %s - %s", from, to);
            String id2 = String.format("%s-%s", to, from);
            String name2 = String.format("Train %s - %s", to, from);
            int seats = rand.nextInt(1000) + 1;

            Node node1 = new TrainNode(id1, name1, 0, 0, time, seats);
            Node node2 = new TrainNode(id2, name2, 0, 0, time, seats);
            try {
                map.add(node1, from, to);
                map.add(node2, to, from);
                trainCount++;
                LOGGER.info(String.format("Train Node %s (%s -> %s) added (%d seats)", id1, from, to, seats));
            } catch (Exception pEx) {
                // either from or to do not exist
            }
        }

        LOGGER.info(String.format("%d stations added / %d trains added", stationCount, trainCount));
    } catch (Exception pEx) {
        throw new Exception("Could not build map", pEx);
    }

    return map;
}