List of usage examples for org.json.simple JSONArray isEmpty
public boolean isEmpty()
From source file:dependencies.DependencyResolving.java
/** * @param args the command line arguments *///from w w w .j av a 2s . com public static void main(String[] args) { // TODO code application logic here JSONParser parser = new JSONParser(); //we use JSONParser in order to be able to read from JSON file try { //here we declare the file reader and define the path to the file dependencies.json Object obj = parser.parse(new FileReader( "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\dependencies.json")); JSONObject project = (JSONObject) obj; //a JSON object containing all the data in the .json file JSONArray dependencies = (JSONArray) project.get("dependencies"); //get array of objects with key "dependencies" System.out.print("We need to install the following dependencies: "); Iterator<String> iterator = dependencies.iterator(); //define an iterator over the array "dependencies" while (iterator.hasNext()) { System.out.println(iterator.next()); } //on the next line we declare another object, which parses a Parser object and reads from all_packages.json Object obj2 = parser.parse(new FileReader( "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\all_packages.json")); JSONObject tools = (JSONObject) obj2; //a JSON object containing all thr data in the file all_packages.json for (int i = 0; i < dependencies.size(); i++) { if (tools.containsKey(dependencies.get(i))) { System.out.println( "In order to install " + dependencies.get(i) + ", we need the following programs:"); JSONArray temporaryArray = (JSONArray) tools.get(dependencies.get(i)); //a temporary JSON array in which we store the keys and values of the dependencies for (i = 0; i < temporaryArray.size(); i++) { System.out.println(temporaryArray.get(i)); } ArrayList<Object> arraysOfJsonData = new ArrayList<Object>(); //an array in which we will store the keys of the objects, after we use the values and won't need them anymore for (i = 0; i < temporaryArray.size(); i++) { System.out.println("Installing " + temporaryArray.get(i)); } while (!temporaryArray.isEmpty()) { for (Object element : temporaryArray) { if (tools.containsKey(element)) { JSONArray secondaryArray = (JSONArray) tools.get(element); //a temporary array within the scope of the if-statement if (secondaryArray.size() != 0) { System.out.println("In order to install " + element + ", we need "); } for (i = 0; i < secondaryArray.size(); i++) { System.out.println(secondaryArray.get(i)); } for (Object o : secondaryArray) { arraysOfJsonData.add(o); //here we create a file with the installed dependency File file = new File( "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\" + o); if (file.createNewFile()) { System.out.println(file.getName() + " is installed!"); } else { } } secondaryArray.clear(); } } temporaryArray.clear(); for (i = 0; i < arraysOfJsonData.size(); i++) { temporaryArray.add(arraysOfJsonData.get(i)); } arraysOfJsonData.clear(); } } } Set<String> keys = tools.keySet(); // here we define a set of keys of the objects in all_packages.json for (String s : keys) { File file = new File( "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\" + s); if (file.createNewFile()) { System.out.println(file.getName() + " is installed."); } else { } } } catch (IOException ex) { Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:modelo.ApiManager.java
public static void limpiarJSONArray(JSONArray jsonArray) { if (!jsonArray.isEmpty()) { jsonArray.clear(); } }
From source file:MyTest.ParseJson.java
private static void getDetailInfo(JSONObject jstep_detail) { // System.out.println(jstep_detail); long id = (long) jstep_detail.get("id"); System.out.println("id:" + id); String name = (String) jstep_detail.get("name"); System.out.println("name:" + name); JSONArray inputs = (JSONArray) jstep_detail.get("inputs"); if (inputs.isEmpty()) { System.out.println("inputs:null"); } else {//w ww . j a v a 2 s . c o m System.out.println("inputs:"); for (int i = 0; i < inputs.size(); i++) { System.out.println(inputs.get(i) + "\n"); } } JSONArray outpus = (JSONArray) jstep_detail.get("outputs"); if (outpus.isEmpty()) { System.out.println("outpus:null"); } else { System.out.println("outpus:"); for (int i = 0; i < outpus.size(); i++) { System.out.print(outpus.get(i) + "\n"); } } JSONObject links = (JSONObject) jstep_detail.get("input_connections"); if (links.isEmpty()) { System.out.println("input_connections:null"); } else { Iterator it = links.keySet().iterator(); System.out.println("input_connections: "); while (it.hasNext()) { Object input_link = it.next(); System.out.println(links.get(input_link)); } } System.out.println("-------------------------------------------------------"); }
From source file:me.fromgate.messagecommander.PLListener.java
private static String jsonToString(String json) { JSONObject jsonObject = (JSONObject) JSONValue.parse(json); if (jsonObject == null || json.isEmpty()) return json; JSONArray array = (JSONArray) jsonObject.get("extra"); if (array == null || array.isEmpty()) return json; return jsonToString(array); }
From source file:cloudclient.Client.java
@SuppressWarnings("unchecked") public static void batchSendTask(PrintWriter out, String workload) throws FileNotFoundException, MalformedURLException { FileInputStream input = new FileInputStream(workload); BufferedReader bin = new BufferedReader(new InputStreamReader(input)); // Get task from workload file String line;//from w w w . ja va 2 s . com // String sleepLength; String id; int count = 0; final int batchSize = 10; try { //Get client public IP String ip = getIP(); //System.out.println(ip); //JSON object Array JSONArray taskList = new JSONArray(); while ((line = bin.readLine()) != null) { //sleepLength = line.replaceAll("[^0-9]", ""); //System.out.println(sleepLength); count++; id = ip + ":" + count; JSONObject task = new JSONObject(); task.put("task_id", id); task.put("task", line); taskList.add(task); if (taskList.size() == batchSize) { out.println(taskList.toString()); taskList.clear(); } } //System.out.println(taskList.toString()); if (!taskList.isEmpty()) { out.println(taskList.toString()); taskList.clear(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.googlecode.fascinator.common.JsonSimple.java
/** * <p>// w w w . jav a2 s .c o m * Take all of the JsonObjects found in a JSONArray, wrap them in * JsonSimple objects, then add to a Java list and return. * </p> * * All entries found that are not JsonObjects are ignored. * * @return String : The JSON String */ public static List<JsonSimple> toJavaList(JSONArray array) { List<JsonSimple> response = new LinkedList<JsonSimple>(); if (array != null && !array.isEmpty()) { for (Object object : array) { if (object != null && object instanceof JsonObject) { response.add(new JsonSimple((JsonObject) object)); } } } return response; }
From source file:com.ecofactor.qa.automation.consumerapi.ECPCoreEnergyPricing_Test.java
/** * APPS-246 Energy pricing on valid ecp core. * @param username the username/*from ww w .ja v a2 s. c om*/ * @param password the password * @param locationId the location id */ @Test(groups = { Groups.SANITY1, Groups.BROWSER }, dataProvider = "validecpcore", dataProviderClass = ApiDataProvider.class, priority = 1) public void energyPricingOnValidEcpCore(final String username, final String password, final String ecpcoreId) { setLogString("Verify energy pricing for valid ecp core id.", true); final Response response = consumerApiURL.getECPCoreEnergySavings(ecpcoreId, securityCookie); setLogString("Response :'" + response + "'", true); final String content = response.readEntity(String.class); setLogString("Json Response:", true, CustomLogLevel.MEDIUM); setLogString(content, true, CustomLogLevel.MEDIUM); final JSONObject jsonObject = JsonUtil.parseObject(content); final JSONArray gasJsonArray = (JSONArray) jsonObject.get("gas"); final JSONArray electricJsonArray = (JSONArray) jsonObject.get("electric"); Assert.assertTrue(!gasJsonArray.isEmpty() || !electricJsonArray.isEmpty(), "Energy savings not exists for given ecp core."); setLogString("Verified energy pricing for valid ecp core id.", true); }
From source file:ca.fastenalcompany.jsonconfig.ProductJson.java
@GET @Produces("application/json") public String doGet(@PathParam("id") Integer id) { query(PropertyManager.getProperty("db_selectAll")).toString(); JSONArray products = query(PropertyManager.getProperty("db_select"), id + ""); return products.isEmpty() ? new JSONObject().toString() : products.get(0).toString(); }
From source file:gov.nih.nci.ispy.web.ajax.ReporterLookup.java
/** * this ajax method looks up reporters using gexpannotation service * based on gene symbol and array platform. An addition parameter is * provided so that the callback(javascript) knows where to put the collection * This last paramter is an id from the ui element. * NOTE: the id passed in WILL be used in the callback...make sure it is there! *//*from ww w .java 2s . c o m*/ public String lookup(String gene, String arrayPlatform, String uiElement) { Collection<String> reporters = new HashSet<String>(); //set up geneCollection Collection<String> genes = new ArrayList<String>(); genes.add(gene); //set up array platform by finding the appropriate type String[] uiString = arrayPlatform.split("#"); String myClassName = uiString[0]; String myValueName = uiString[1]; Enum myType = EnumHelper.createType(myClassName, myValueName); if (myType instanceof ArrayPlatformType) { GeneExprAnnotationService gs = GeneExprAnnotationServiceFactory.getInstance(); reporters = gs.getReporterNamesForGeneSymbols(genes, (ArrayPlatformType) myType); } /* reporterResultArray variable holds reporter objects like the result * list of reporters (in its own array called reporterList), * the results String, and the UIElement */ JSONArray reporterResultArray = new JSONArray(); JSONObject resultsObject = new JSONObject(); JSONArray reporterList = new JSONArray(); for (String reporter : reporters) { reporterList.add(reporter); } String results = "notFound"; if (!reporterList.isEmpty()) { results = "found"; } resultsObject.put("gene", gene); resultsObject.put("results", results); resultsObject.put("reporters", reporterList); resultsObject.put("elementId", uiElement); reporterResultArray.add(resultsObject); return reporterResultArray.toString(); }
From source file:com.p000ison.dev.simpleclans2.updater.bamboo.BambooBuild.java
@Override public void fetchInformation() throws IOException, FailedBuildException { Reader reader;/*from w w w . j a va 2 s . c o m*/ JSONObject content; String latestBuild = updateType == UpdateType.LATEST ? String.format(LATEST_BUILD, job) : String.format(LATEST_BUILD_BY_LABEL, job, updateType.toString()); try { reader = connect(latestBuild); } catch (FileNotFoundException e) { Logging.debug("No build found in this channel!"); return; } content = parseJSON(reader); JSONObject results = (JSONObject) content.get("results"); JSONArray result = (JSONArray) results.get("result"); if (result.isEmpty()) { Logging.debug("No build found in this channel!"); return; } JSONObject firstResult = (JSONObject) result.get(0); int buildNumberToFetch = ((Long) firstResult.get("number")).intValue(); String state = (String) firstResult.get("state"); if (!state.equalsIgnoreCase("Successful")) { throw new FailedBuildException("The last build failed!"); } reader.close(); reader = connect(String.format(API_FILE, job, buildNumberToFetch)); content = parseJSON(reader); try { this.started = DATE_FORMATTER.parse((String) content.get("buildStartedTime")).getTime(); } catch (ParseException e) { e.printStackTrace(); } this.duration = (Long) content.get("buildDuration"); this.buildNumber = ((Long) content.get("number")).intValue(); this.commitId = (String) content.get("vcsRevisionKey"); JSONArray changes = (JSONArray) ((JSONObject) content.get("changes")).get("change"); if (!changes.isEmpty()) { JSONObject change = (JSONObject) changes.get(0); pusher = (String) change.get("userName"); } reader.close(); }