Example usage for org.json.simple JSONArray size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:com.turt2live.uuid.turt2live.v1.ApiV1Service.java

@Override
public String[] getNameHistory(UUID uuid) {
    String response = doUrlRequest(getConnectionUrl() + "/history/" + convertUuid(uuid));
    if (response != null) {
        JSONObject json = (JSONObject) JSONValue.parse(response);

        if (json.containsKey("names")) {
            JSONArray array = (JSONArray) json.get("names");
            String[] names = new String[array.size()];

            int i = 0;
            for (Object o : array) {
                names[i] = o.toString();
                i++;//from   w w w. j  a  va 2  s  .c om
            }

            return names;
        }
    }

    return null;
}

From source file:control.ProcesoVertimientosServlets.InsertarProgramacionMonitoreo.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from w  w w  . j a va 2 s.co  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    JSONObject respError = new JSONObject();

    try {

        //Obtenemos el numero de contrato
        String consultorMonitoreo = request.getParameter("consultorMonitoreo");
        String fechaMonitoreo = request.getParameter("fechaMonitoreo");
        String horaInicioMonitoreo = request.getParameter("horaInicioMonitoreo");
        String horaFinMonitoreo = request.getParameter("horaFinMonitoreo");
        int laboratorioMonitoreo = Integer.parseInt(request.getParameter("laboratorioMonitoreo"));
        int codigoProceso = Integer.parseInt(request.getParameter("codigoProceso"));
        String observacion = request.getParameter("observacionesReprogramacion");
        String duracionMonitoreo = request.getParameter("duracionMonitoreo");

        //Insertamos el programacion del monitoreo y obtenemos el codigo
        ProgramarMonitoreo manager = new ProgramarMonitoreo();
        int codigoMonitoreo = manager.insertar(consultorMonitoreo, fechaMonitoreo, horaInicioMonitoreo,
                horaFinMonitoreo, laboratorioMonitoreo, codigoProceso, observacion, duracionMonitoreo);

        //Obtenemos la cadena con la informacion y la convertimos en un
        //JSONArray
        String puntos = request.getParameter("puntosVertimiento");
        Object obj = JSONValue.parse(puntos);
        JSONArray jsonArray = new JSONArray();
        jsonArray = (JSONArray) obj;

        //Recorremos el JSONArray y obtenemos la informacion.
        for (int i = 0; i < jsonArray.size(); i++) {

            //Obtenemos la info de los puntos
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            int codigoPunto = Integer.parseInt((String) jsonObject.get("codigo"));
            int codigoActividad = Integer.parseInt((String) jsonObject.get("actividad"));

            //Creamos el manager y guardamos la informacion.
            manager.insertarPuntoMonitoreo(codigoPunto, codigoActividad, codigoMonitoreo);

        }

        respError.put("error", "1");
        response.getWriter().write(respError.toString());

    } catch (Exception ex) {

        respError.put("error", "0");
        response.getWriter().write(respError.toString());

    }

}

From source file:com.optimizely.ab.config.parser.JsonSimpleConfigParser.java

private List<EventType> parseEvents(JSONArray eventJson) {
    List<EventType> events = new ArrayList<EventType>(eventJson.size());

    for (Object obj : eventJson) {
        JSONObject eventObject = (JSONObject) obj;
        JSONArray experimentIdsJson = (JSONArray) eventObject.get("experimentIds");
        List<String> experimentIds = new ArrayList<String>(experimentIdsJson.size());

        for (Object experimentIdObj : experimentIdsJson) {
            experimentIds.add((String) experimentIdObj);
        }/*from  www .  ja  v  a2s  .co m*/

        String id = (String) eventObject.get("id");
        String key = (String) eventObject.get("key");

        events.add(new EventType(id, key, experimentIds));
    }

    return events;
}

From source file:com.optimizely.ab.config.parser.JsonSimpleConfigParser.java

private List<Variation> parseVariations(JSONArray variationJson) {
    List<Variation> variations = new ArrayList<Variation>(variationJson.size());

    for (Object obj : variationJson) {
        JSONObject variationObject = (JSONObject) obj;
        String id = (String) variationObject.get("id");
        String key = (String) variationObject.get("key");

        variations.add(new Variation(id, key));
    }//from   ww w  . j a  v a 2 s. c  o m

    return variations;
}

From source file:com.optimizely.ab.config.parser.JsonSimpleConfigParser.java

private List<Attribute> parseAttributes(JSONArray attributeJson) {
    List<Attribute> attributes = new ArrayList<Attribute>(attributeJson.size());

    for (Object obj : attributeJson) {
        JSONObject attributeObject = (JSONObject) obj;
        String id = (String) attributeObject.get("id");
        String key = (String) attributeObject.get("key");
        String segmentId = (String) attributeObject.get("segmentId");

        attributes.add(new Attribute(id, key, segmentId));
    }/* w  ww.j a  v  a  2 s .  c  om*/

    return attributes;
}

From source file:gui.WvWMatchReader.java

@Override
public void run() {

    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 1000).build();
    HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
    //HttpClient client = new DefaultHttpClient();

    HttpGet request = new HttpGet("https://api.guildwars2.com/v1/wvw/matches.json");

    HttpResponse response;/* w w  w . j  a  va  2s  .  co m*/

    String line = "";
    String out = "";

    while (!this.isInterrupted()) {

        try {

            response = client.execute(request);

            if ((response.getStatusLine().toString().contains("200"))) {

                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")));

                line = "";
                out = "";

                while ((line = rd.readLine()) != null) {

                    out = out + line;
                }

                JSONParser parser = new JSONParser();

                Object obj;

                this.result.clear();

                try {

                    obj = parser.parse(out);

                    JSONObject obj2 = (JSONObject) obj;
                    JSONArray array = (JSONArray) obj2.get("wvw_matches");

                    for (int i = 0; i < array.size(); i++) {

                        obj2 = (JSONObject) array.get(i);

                        this.result.put("" + obj2.get("wvw_match_id"),
                                new String[] { "" + obj2.get("red_world_id"), "" + obj2.get("blue_world_id"),
                                        "" + obj2.get("green_world_id") });
                    }

                    request.releaseConnection();

                    this.wvwCheckBox.setEnabled(true);
                    this.interrupt();
                } catch (ParseException ex) {
                    try {
                        Logger.getLogger(ApiManager.class.getName()).log(Level.SEVERE, null, ex);

                        request.releaseConnection();
                        Thread.sleep(3000);
                    } catch (InterruptedException ex1) {
                        Logger.getLogger(WvWMatchReader.class.getName()).log(Level.SEVERE, null, ex1);
                    }
                }
            } else {
                try {
                    Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, "Connection error.");

                    request.releaseConnection();
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(HomeWorldAllReader.class.getName()).log(Level.SEVERE, null, ex);

                    this.interrupt();
                }
            }
        } catch (IOException | IllegalStateException ex) {
            try {
                Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex);

                request.releaseConnection();
                Thread.sleep(3000);
            } catch (InterruptedException ex1) {
                Logger.getLogger(HomeWorldAllReader.class.getName()).log(Level.SEVERE, null, ex1);

                this.interrupt();
            }
        }
    }
}

From source file:com.respam.comniq.models.ExcelExporter.java

public void addImages(JSONArray parsedArr) throws IOException, BiffException, WriteException {

    for (int i = 0; i < parsedArr.size(); i++) {

        String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output";
        File file = new File(path + File.separator + "movieInfo.xls");
        Workbook outFile = Workbook.getWorkbook(file);

        String thumbnailPath = System.getProperty("user.home") + File.separator + "comniq" + File.separator
                + "output" + File.separator + "thumbnails";

        WritableImage poster;// w  w w.ja  va  2  s . c o  m
        WritableWorkbook workbook = Workbook.createWorkbook(file, outFile);
        WritableSheet excelSheet = workbook.getSheet("Movies");
        JSONObject jsonObj = (JSONObject) parsedArr.get(i);
        File thumbnailFile = new File(thumbnailPath + File.separator + jsonObj.get("Title") + ".png");
        poster = new WritableImage(0, i + 1, 1, 1, thumbnailFile);

        excelSheet.addImage(poster);
        workbook.write();
        workbook.close();
        System.out.println("Added " + jsonObj.get("Title"));
    }

}

From source file:com.acmeair.jmeter.functions.FlightsPostProcessor.java

public String processJSonString(String responseDataAsString) {

    try {/* w w  w  .ja  va2  s .  c om*/
        JSONObject json = (JSONObject) new JSONParser().parse(responseDataAsString);

        JSONArray tripFlights = (JSONArray) json.get("tripFlights");

        if (tripFlights == null || tripFlights.size() == 0) {

            context.setIsFlightAvailable("false");
            FlightsThreadLocal.set(context);
            return null;
        }
        for (int counter = 1; counter <= tripFlights.size(); counter++) {
            if (counter == 1) {
                JSONObject jsonTripFlight = (JSONObject) tripFlights.get(0);
                JSONArray jsonFlightOptions = (JSONArray) jsonTripFlight.get("flightsOptions");

                String numFlightsAsString = jsonFlightOptions.size() + "";
                context.setNumOfToFlights(numFlightsAsString);
                JMeterContextService.getContext().getVariables().put(FLIGHT_TO_COUNT, numFlightsAsString);
                if (jsonFlightOptions.size() > 0) {
                    context.setIsFlightAvailable("true");
                } else {
                    context.setIsFlightAvailable("false");
                }
            } else if (counter == 2) {
                JSONObject jsonTripFlight = (JSONObject) tripFlights.get(1);
                JSONArray jsonFlightOptions = (JSONArray) jsonTripFlight.get("flightsOptions");
                String numFlightAsString = jsonFlightOptions.size() + "";
                context.setNumOfRetFlights(numFlightAsString);
                JMeterContextService.getContext().getVariables().put(FLIGHT_RET_COUNT, numFlightAsString);
                if (jsonFlightOptions.size() > 0) {
                    context.setONEWAY("false");
                    JMeterContextService.getContext().getVariables().put(ONE_WAY, "false");
                } else {
                    context.setONEWAY("true");
                    JMeterContextService.getContext().getVariables().put(ONE_WAY, "true");
                }
            }

            JSONObject jsonTripFlight = (JSONObject) tripFlights.get(counter - 1);
            JSONArray jsonFlightOptions = (JSONArray) jsonTripFlight.get("flightsOptions");

            for (int tripCounter = 1; tripCounter <= jsonFlightOptions.size(); tripCounter++) {
                if (counter == 1) {
                    JSONObject flightOption0 = (JSONObject) jsonFlightOptions.get(tripCounter - 1);
                    if (ExtractFlightsInfoFunction.pureIDs) {
                        String id = (String) flightOption0.get("_id");
                        String fsId = (String) flightOption0.get("flightSegmentId");
                        context.setTOFLIGHT(id);
                        context.setTOSEGMENTID(fsId);
                    } else {
                        JSONObject flightOption0Pkey = (JSONObject) flightOption0.get("pkey");
                        context.setTOFLIGHT((String) flightOption0Pkey.get("id"));
                        context.setTOSEGMENTID((String) flightOption0Pkey.get("flightSegmentId"));
                    }
                } else if (counter == 2) {
                    JSONObject flightOption0 = (JSONObject) jsonFlightOptions.get(tripCounter - 1);
                    if (ExtractFlightsInfoFunction.pureIDs) {
                        String id = (String) flightOption0.get("_id");
                        String fsId = (String) flightOption0.get("flightSegmentId");
                        context.setRETFLIGHT(id);
                        context.setRESEGMENTID(fsId);
                    } else {
                        JSONObject flightOption0Pkey = (JSONObject) flightOption0.get("pkey");
                        context.setRETFLIGHT((String) flightOption0Pkey.get("id"));
                        context.setRESEGMENTID((String) flightOption0Pkey.get("flightSegmentId"));
                    }
                }
            }
        }
        FlightsThreadLocal.set(context);

        return json.toJSONString();

    } catch (ParseException e) {
        System.out.println("responseDataAsString = " + responseDataAsString);
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
        System.out.println(
                "NullPointerException in FlightsPostProcessor - ResponseData =" + responseDataAsString);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:importer.handler.post.stages.SAXSplitter.java

/**
 * Sort an array of path objects/*ww w .  ja v a2s. c  o  m*/
 * @param jArr the array of path json objects
 * @return the sorted array
 */
JSONArray sort(JSONArray jArr) {
    int increment = jArr.size() / 2;
    while (increment > 0) {
        for (int i = increment; i < jArr.size(); i++) {
            int j = i;
            JSONObject temp = (JSONObject) jArr.get(i);
            while (j >= increment && compare((JSONObject) jArr.get(j - increment), temp) > 0) {
                jArr.set(j, jArr.get(j - increment));
                j = j - increment;
            }
            jArr.set(j, temp);
        }
        if (increment == 2)
            increment = 1;
        else
            increment *= (5.0 / 11);
    }
    return jArr;
}

From source file:me.timothy.ddd.quests.QuestManager.java

@SuppressWarnings("unchecked")
public QuestManager(Player player, AchievementManager aManager, EntityManager entManager) {
    logger = LogManager.getLogger();
    entityManager = entManager;/*from ww  w  .  j av  a 2s.  com*/
    achievementManager = aManager;
    acceptedQuests = new ArrayList<>();
    completedQuests = new ArrayList<>();
    this.player = player;

    File questsFile = new File("quests.json");
    if (questsFile.exists()) {
        try (FileReader fr = new FileReader(new File("quests.json"))) {
            JSONObject jObj = (JSONObject) (new JSONParser().parse(fr));

            JSONArray questsArr = (JSONArray) jObj.get("current");
            for (int i = 0; i < questsArr.size(); i++) {
                JSONObject questObj = (JSONObject) questsArr.get(i);
                String classStr = (String) questObj.get("class");
                Class<?> cl = Class.forName(classStr);
                Quest quest = null;
                try {
                    quest = (Quest) cl.getMethod("fromObject", getClass(), JSONObject.class).invoke(null, this,
                            questObj);
                } catch (NoSuchMethodException nsme) {
                    quest = (Quest) cl.getConstructor(QuestManager.class).newInstance(this);
                }
                acceptedQuests.add(quest);
            }

            JSONArray complete = (JSONArray) jObj.get("complete");
            for (int i = 0; i < complete.size(); i++) {
                //               completedQuests.add((Class<? extends Quest>) Class.forName((String) complete.get(i)));
            }
        } catch (Exception e) {
            logger.catching(e);
        }
    }

    Runnable saveQuests = new Runnable() {

        @Override
        public void run() {
            if (!DrunkDuckDispatch.ddd.save)
                return;
            JSONObject jObj = new JSONObject();
            JSONArray questsArr = new JSONArray();
            for (Quest qu : acceptedQuests) {
                questsArr.add(qu.asObject());
            }
            jObj.put("current", questsArr);
            JSONArray completed = new JSONArray();
            for (Class<? extends Quest> cl : completedQuests) {
                completed.add(cl.getCanonicalName());
            }
            jObj.put("complete", completed);
            try (FileWriter fw = new FileWriter(new File("quests.json"))) {
                jObj.writeJSONString(fw);
            } catch (IOException ie) {

            }
        }

    };

    Runtime.getRuntime().addShutdownHook(new Thread(saveQuests));
}