Example usage for org.json.simple JSONArray get

List of usage examples for org.json.simple JSONArray get

Introduction

In this page you can find the example usage for org.json.simple JSONArray get.

Prototype

public E get(int index) 

Source Link

Document

Returns the element at the specified position in this list.

Usage

From source file:iracing.webapi.LicenseGroupParser.java

public static List<LicenseGroup> parse(String json) {
    JSONParser parser = new JSONParser();
    List<LicenseGroup> output = null;
    try {//from   w w  w  .  j  a v a 2s.c  o  m
        JSONArray rootArray = (JSONArray) parser.parse(json);
        output = new ArrayList<LicenseGroup>();
        for (int i = 0; i < rootArray.size(); i++) {
            JSONObject r = (JSONObject) rootArray.get(i);
            LicenseGroup lg = new LicenseGroup();
            lg.setId(getInt(r, "group"));
            lg.setName(getString(r, "name", true));
            // NOTE: the following aren't specified if not applicable
            Object o = r.get("minNumTT");
            if (o != null)
                lg.setMinimumNumberOfTimeTrials(((Long) o).intValue());
            o = r.get("minNumRaces");
            if (o != null)
                lg.setMinimumNumberOfRaces(((Long) o).intValue());
            output.add(lg);
        }
    } catch (ParseException ex) {
        Logger.getLogger(LicenseGroupParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:iracing.webapi.SeasonStandingsParser.java

public static long parse(String json, ItemHandler handler) {
    JSONParser parser = new JSONParser();
    //        System.err.println(json);
    long output = 0;
    try {//from   w  w w  .  j  a v  a 2s. co  m
        JSONObject root = (JSONObject) parser.parse(json);
        JSONObject arrayRoot = (JSONObject) root.get("d");
        output = getLong(arrayRoot, "27");
        JSONArray results = (JSONArray) arrayRoot.get("r");
        for (int i = 0; i < results.size(); i++) {
            JSONObject result = (JSONObject) results.get(i);
            SeasonStanding standing = new SeasonStanding();
            standing.setDroppedWeeks(getInt(result, "1"));
            standing.setClubName(getString(result, "2", true));
            standing.setCountryCode(getString(result, "3", true));
            standing.setLicenseSubLevel(getString(result, "4"));
            standing.setAverageFinish(getInt(result, "5"));
            standing.setIrating(getInt(result, "6"));
            standing.setTotalTopFives(getInt(result, "7"));
            standing.setMaxLicenseLevel(getInt(result, "8"));
            standing.setDriverName(getString(result, "9", true));
            standing.setClubId(getInt(result, "10"));
            standing.setTotalStarts(getInt(result, "11"));
            standing.setDisplayCountry(getString(result, "14", true));
            standing.setTotalLapsLed(getInt(result, "13"));
            standing.setCountry(getString(result, "15", true));
            standing.setTotalWins(getInt(result, "16"));
            standing.setTotalIncidents(getInt(result, "17"));
            Object o = result.get("18");
            double d;
            if (o instanceof Long) {
                Long l = (Long) o;
                d = l.doubleValue();
            } else {
                d = (Double) o;
            }
            standing.setTotalPoints(d);
            standing.setRank(getInt(result, "20"));
            standing.setDivision(getInt(result, "22"));
            standing.setDriverCustomerId(getLong(result, "24"));
            standing.setWeeksCounted(getInt(result, "26"));
            standing.setTotalLaps(getLong(result, "28"));
            standing.setAverageStart(getInt(result, "29"));
            standing.setTotalPoles(getInt(result, "30"));
            standing.setPosition(getLong(result, "31"));
            if (!handler.onSeasonStandingParsed(standing))
                break;
        }
    } catch (ParseException ex) {
        Logger.getLogger(SeasonStandingsParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:it.polimi.proximityapi.jsonLogic.POIJsonParser.java

public static ArrayList<POI> parsePOIFile(String jsonString) {
    ArrayList<POI> poiList = new ArrayList<>();
    JSONParser parser = new JSONParser();

    JSONArray poiArray;
    try {/* ww  w. j a v  a  2s  . c o  m*/
        poiArray = (JSONArray) parser.parse(jsonString);
        for (int i = 0; i < poiArray.size(); i++) {
            JSONObject jsonPOI = (JSONObject) poiArray.get(i);
            POI poi = new POI();
            poi.setName((String) jsonPOI.get(JsonStrings.NAME));
            poi.setLatitude(Double.parseDouble((String) jsonPOI.get(JsonStrings.LATITUDE)));
            poi.setLongitude(Double.parseDouble((String) jsonPOI.get(JsonStrings.LONGITUDE)));
            if (!jsonPOI.get(JsonStrings.RADIUS).equals(""))
                poi.setRadius(Float.parseFloat((String) jsonPOI.get(JsonStrings.RADIUS)));
            else {
                //Default radius set when it is not set in the corresponding JSON object
                poi.setRadius(TechnologyManager.BT_START_GEOFENCE_RADIUS);
            }
            poi.setBeaconUuid((String) jsonPOI.get(JsonStrings.BEACON_UUID));
            poiList.add(poi);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return poiList;
}

From source file:mml.handler.json.Dialect.java

private static boolean compareArrays(JSONArray a1, JSONArray a2) {
    boolean res = true;
    if (a1.size() == a2.size()) {
        for (int i = 0; i < a1.size(); i++) {
            Object o1 = a1.get(i);
            Object o2 = a2.get(i);
            if (o1 != null && o2 != null && o1.getClass().equals(o2.getClass())) {
                if (o1 instanceof JSONObject)
                    res = compareObjects((JSONObject) o1, (JSONObject) o2);
                else if (o1 instanceof Number)
                    res = o1.equals(o2);
                else if (o1 instanceof String)
                    res = o1.equals(o2);
                else if (o1 instanceof JSONArray)
                    res = compareArrays((JSONArray) o1, (JSONArray) o2);
                else
                    res = false;//from w w  w . ja  v a  2  s. c  om
            } else
                res = false;
        }
    } else
        res = false;
    return res;
}

From source file:cloudclient.Client.java

public static void batchReceiveResp(BufferedReader in) throws IOException, ParseException {
    BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));

    JSONParser parser = new JSONParser();

    String message;/*from www.  j  a v a  2 s.co m*/
    while ((message = in.readLine()) != null) {
        //System.out.println(message);
        JSONArray responseList = (JSONArray) parser.parse(message);

        for (int i = 0; i < responseList.size(); i++) {
            JSONObject response = (JSONObject) responseList.get(i);
            bw.write(response.get("URL").toString());
            bw.newLine();
        }
    }

    bw.close();

}

From source file:com.ijuru.ijambo.Context.java

/**
 * Connects to the database//  w ww.ja v a 2 s . co  m
 * @return the Mongo DB
 * @throws ParseException
 * @throws UnknownHostException
 */
public static DB connectDatabase() throws ParseException, UnknownHostException {

    // Check for AppFog environment
    String appFogEnv = System.getenv("VCAP_SERVICES");

    if (appFogEnv != null) {
        // Connect to MongoDB as AppFog service
        JSONParser parser = new JSONParser();
        JSONObject svcs = (JSONObject) parser.parse(appFogEnv);
        JSONArray mongoSettings = (JSONArray) svcs.get("mongodb-1.8");
        JSONObject mongoSettings0 = (JSONObject) mongoSettings.get(0);
        JSONObject mongoCreds0 = (JSONObject) mongoSettings0.get("credentials");
        //String db = (String)mongoCreds0.get("db");
        String connectionURL = (String) mongoCreds0.get("url");

        log.info("Running as AppFog instance with connection URL: " + connectionURL);

        DB db = new MongoURI(connectionURL).connectDB();

        // https://jira.mongodb.org/browse/JAVA-436
        db.authenticate((String) mongoCreds0.get("username"),
                ((String) mongoCreds0.get("password")).toCharArray());

        return db;
    }

    // Create default MongoDB instance
    m = new Mongo();
    return m.getDB("ijambo");
}

From source file:Models.Taxonomy.Repository.RepositoryTNRS.java

/**
 * Method that consuming web services from tnrs and get results
 * @param name Name from specie to evaluate
 * @param best True for best results, false if all results
 * @return //  ww  w  . j a  va 2  s .com
 */
public static TNRS[] get(String name, boolean best) {
    TNRS[] a = null;
    try {
        if (db == null)
            db = new HashMap();
        if (db.containsKey(name.trim().replaceAll(" ", "_")))
            return (TNRS[]) db.get(name.trim().replaceAll(" ", "_"));
        URL url = new URL(Configuration.getParameter("tnrs_url_base")
                + (best ? "retrieve=best" : "retrieve=all") + "&names=" + name.replaceAll(" ", "%20"));
        BufferedReader lector = new BufferedReader(new InputStreamReader(url.openStream()));
        String textJson = lector.readLine();
        if (textJson == null)
            throw new Exception("Don't found item " + name);
        JSONArray jsNames = (JSONArray) ((JSONObject) JSONValue.parse(textJson)).get("items");
        a = new TNRS[jsNames.size()];
        for (int i = 0; i < a.length; i++)
            a[i] = new TNRS((JSONObject) jsNames.get(i));
        db.put(name.trim().replaceAll(" ", "_"), a);
    } catch (Exception ex) {
        a = null;
        System.out.println("Error TNRS: " + ex);
    }
    return a;
}

From source file:iracing.webapi.SeasonTimeTrialStandingsParser.java

public static SeasonTimeTrialStandings parse(String json) {
    JSONParser parser = new JSONParser();
    SeasonTimeTrialStandings output = null;
    try {//from w  ww . j  av  a  2s  . co m
        JSONObject root = (JSONObject) parser.parse(json);
        Object o = root.get("d");
        if (o instanceof JSONObject) {
            JSONObject d = (JSONObject) o;
            output = new SeasonTimeTrialStandings();
            String s = getString(d, "5");
            output.setApiUserRow(Integer.parseInt(s));
            output.setTotalRecords(getLong(d, "18"));
            JSONArray arrayRoot = (JSONArray) d.get("r");
            List<SeasonTimeTrialStanding> standings = new ArrayList<SeasonTimeTrialStanding>();
            for (int i = 0; i < arrayRoot.size(); i++) {
                JSONObject r = (JSONObject) arrayRoot.get(i);
                SeasonTimeTrialStanding standing = new SeasonTimeTrialStanding();
                standing.setDroppedWeeks(getInt(r, "1"));
                standing.setClubName(getString(r, "2", true));
                o = r.get("3");
                if (o instanceof Double) {
                    standing.setTotalPoints((Double) o);
                } else if (o instanceof Long) {
                    standing.setTotalPoints(Double.parseDouble(String.valueOf(o)));
                }
                standing.setLicenseSubLevel(getString(r, "4"));
                standing.setMaxLicenseLevel(getInt(r, "6"));
                standing.setRank(getInt(r, "7"));
                standing.setDivision(getInt(r, "9"));
                standing.setDriverName(getString(r, "10", true));
                standing.setDriverCustomerId(getLong(r, "12"));
                standing.setTotalStarts(getInt(r, "14"));
                standing.setClubId(getInt(r, "15"));
                standing.setWeeksCounted(getInt(r, "17"));
                standing.setTotalWins(getInt(r, "19"));
                standing.setPosition(getInt(r, "20"));
                standings.add(standing);
            }
            output.setStandings(standings);
        }
    } catch (ParseException ex) {
        Logger.getLogger(SeasonTimeTrialStandingsParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.baystep.jukeberry.JsonConfiguration.java

public static String[] getStringArray(JSONObject obj, String key) {
    ArrayList<String> list = new ArrayList<>();
    JSONArray array = (JSONArray) obj.get(key);
    if (array != null) {
        for (int i = 0; i < array.size(); i++) {
            list.add(array.get(i).toString());
        }/*from   w w  w.  ja va2s. c  o  m*/
    }
    String[] output = new String[list.size()];
    list.toArray(output);
    return output;
}

From source file:com.portfolio.data.utils.PostForm.java

public static boolean updateResource(String sessionid, String backend, String uuid, String lang, String json)
        throws Exception {
    /// Parse and create xml from JSON
    JSONObject files = (JSONObject) JSONValue.parse(json);
    JSONArray array = (JSONArray) files.get("files");

    if ("".equals(lang) || lang == null)
        lang = "fr";

    JSONObject obj = (JSONObject) array.get(0);
    String ressource = "";
    String attLang = " lang=\"" + lang + "\"";
    ressource += "<asmResource>" + "<filename" + attLang + ">" + obj.get("name") + "</filename>" + // filename
            "<size" + attLang + ">" + obj.get("size") + "</size>" + "<type" + attLang + ">" + obj.get("type")
            + "</type>" +
            //      obj.get("url");   // Backend source, when there is multiple backend
            "<fileid" + attLang + ">" + obj.get("fileid") + "</fileid>" + "</asmResource>";

    /// Send data to resource
    /// Server + "/resources/resource/file/" + uuid +"?lang="+ lang
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*from  w  w  w  .ja v  a  2 s  .  c o m*/
        HttpPut put = new HttpPut("http://" + backend + "/rest/api/resources/resource/" + uuid);
        put.setHeader("Cookie", "JSESSIONID=" + sessionid); // So that the receiving servlet allow us

        StringEntity se = new StringEntity(ressource);
        se.setContentEncoding("application/xml");
        put.setEntity(se);

        CloseableHttpResponse response = httpclient.execute(put);

        try {
            HttpEntity resEntity = response.getEntity();
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

    return false;
}