List of usage examples for com.google.gson JsonArray get
public JsonElement get(int i)
From source file:RawCollectionsExample.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { Gson gson = new Gson(); Collection collection = new ArrayList(); collection.add("hello"); collection.add(5);//ww w. j a v a 2 s . c om collection.add(new Event("GREETINGS", "guest")); String json = gson.toJson(collection); System.out.println("Using Gson.toJson() on a raw collection: " + json); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonArray(); String message = gson.fromJson(array.get(0), String.class); int number = gson.fromJson(array.get(1), int.class); Event event = gson.fromJson(array.get(2), Event.class); System.out.printf("Using Gson.fromJson() to get: %s, %d, %s", message, number, event); }
From source file:Plugins.java
License:Apache License
public void getPlugins() { try {/*from w w w .j av a 2s.c om*/ Iterator<TwisterPluginInterface> iterator = RunnerPluginsLoader.getPlugins(); TwisterPluginInterface plugin = null; String name; JsonArray pluginsarray; int size; String pluginname; while (iterator.hasNext()) { try { plugin = iterator.next(); } catch (Exception e) { System.out.println("Could not instatiate plugin"); e.printStackTrace(); continue; } name = plugin.getFileName(); pluginsarray = RunnerRepository.getPlugins().getAsJsonArray(); size = pluginsarray.size(); for (int i = 0; i < size; i++) { pluginname = pluginsarray.get(i).getAsString(); if (pluginname.equals(name) && (plugins.get(pluginname) == null)) { plugins.put(pluginname, plugin); break; } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:Plugins.java
License:Apache License
public void copyPreConfiguredPlugins() { try {// www . j ava2s . com JsonArray plugins = RunnerRepository.getPlugins().getAsJsonArray(); int size = plugins.size(); String pluginfile; File pluginsfolder = new File(RunnerRepository.PLUGINSDIRECTORY); String[] localplugins = pluginsfolder.list(); boolean found; for (int i = 0; i < size; i++) { pluginfile = plugins.get(i).getAsString(); found = false; for (String file : localplugins) { if (file.equals(pluginfile)) { File myfile = new File( RunnerRepository.PLUGINSDIRECTORY + RunnerRepository.getBar() + pluginfile); try { ch.cd(RunnerRepository.REMOTEPLUGINSDIR); } catch (Exception e) { System.out.println("Could not get :" + RunnerRepository.REMOTEPLUGINSDIR + " as remote plugins dir"); } try { long remotesize = ch.lstat(pluginfile).getSize(); long localsize = myfile.length(); if (remotesize == localsize) found = true; } catch (Exception e) { e.printStackTrace(); } break; } } if (!found) { copyPlugin(pluginfile); } } } catch (Exception e) { System.out.println("Could not get Plugins Array from local config"); e.printStackTrace(); } }
From source file:Plugins.java
License:Apache License
public void loadRemotePluginList() { String[] downloadedplugins = null; File pluginsfile = new File(RunnerRepository.PLUGINSDIRECTORY); if (pluginsfile.exists()) downloadedplugins = pluginsfile.list(); ArrayList<String> list = getRemotePlugins(); JPanel panel;/* ww w . j a v a 2 s .co m*/ JLabel lname; for (String name : list) { if (name.indexOf(".jar") == -1) continue; final String tempname = name; lname = new JLabel(name); lname.setBackground(Color.WHITE); final MyButton addremove = new MyButton("Download"); addremove.setMyLabel(lname); for (String localfile : downloadedplugins) { if (name.equals(localfile)) { JsonArray pluginsarray; String pluginname; pluginsarray = RunnerRepository.getPlugins().getAsJsonArray(); int size = pluginsarray.size(); for (int i = 0; i < size; i++) { pluginname = pluginsarray.get(i).getAsString(); if (name.equals(pluginname)) { addremove.setText("Remove"); break; } } } } if (addremove.getText().equals("Remove")) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; if (PermissionValidator.canChangePlugins()) { localtable.add(addremove, gridBagConstraints); } gridBagConstraints.gridx = 0; localtable.add(lname, gridBagConstraints); int height = localtable.getComponentCount() * 40; localtable.setPreferredSize(new Dimension(240, height)); } else { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; if (PermissionValidator.canChangePlugins()) { remotetable2.add(addremove, gridBagConstraints); } gridBagConstraints.gridx = 0; remotetable2.add(lname, gridBagConstraints); } addremove.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { addRemovePlugin(addremove, tempname); } }); } }
From source file:Plugins.java
License:Apache License
public static void deletePlugins() { File pluginsdirectory = new File(RunnerRepository.PLUGINSDIRECTORY); File[] downloadedplugins = pluginsdirectory.listFiles(); boolean found; int size;//from w ww . java2 s.com String plugin; for (File availableplugin : downloadedplugins) { found = false; JsonArray plugins = null; try { plugins = RunnerRepository.getPlugins().getAsJsonArray(); } catch (Exception e) { System.out.println("Plugins list from config file is empty "); return; } size = plugins.size(); for (int i = 0; i < size; i++) { plugin = plugins.get(i).getAsString(); // if(!availableplugin.getName().equals("Twister.jar") && // availableplugin.getName().equals(plugin)){ if (availableplugin.getName().equals(plugin.substring(0, plugin.indexOf(".")) + "_description.txt") || availableplugin.getName().equals(plugin)) { found = true; break; } } if (!found) { availableplugin.delete(); } } }
From source file:GeoLocation.java
public static void main(String args[]) { try {//from w w w.j av a2 s .co m URL myURL = new URL( "https://maps.googleapis.com/maps/api/geocode/json?latlng=17.451425,%2078.380957&key=AIzaSyDe_RBKl4DBGy6YCWRPXi9eQzYACt05Tr8"); URLConnection myURLConnection = myURL.openConnection(); myURLConnection.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = in.readLine()) != null) { sb.append(line + "\n"); } JsonElement json = new JsonParser().parse(sb.toString()); JsonObject j = json.getAsJsonObject(); JsonElement je = j.get("results"); JsonArray ja = je.getAsJsonArray(); JsonElement je1 = ja.get(1); JsonObject j1 = je1.getAsJsonObject(); JsonElement je2 = j1.get("formatted_address"); System.out.println(je2.getAsString()); } catch (MalformedURLException e) { // new URL() failed // ... } catch (IOException e) { // openConnection() failed // ... } }
From source file:EAIJSONConverter.java
License:Open Source License
public static SiebelPropertySet JsonObjectToPropertySet(JsonObject obj, SiebelPropertySet ps) { Iterator<Entry<String, JsonElement>> iterator = obj.entrySet().iterator(); SiebelPropertySet child;//from w ww . j ava 2 s .c o m while (iterator.hasNext()) { JsonArray jsonArray = new JsonArray(); JsonObject jsonObject = new JsonObject(); Map.Entry mapEntry = (Map.Entry) iterator.next(); if (mapEntry != null) { JsonElement jsonelement = (JsonElement) mapEntry.getValue(); if (jsonelement.isJsonArray()) { jsonArray = jsonelement.getAsJsonArray(); child = new SiebelPropertySet(); child.setType("ListOf-" + mapEntry.getKey().toString()); for (int i = 0; i < jsonArray.size(); i++) { if (jsonArray.get(i).isJsonObject() || jsonArray.get(i).isJsonArray()) { SiebelPropertySet temp = new SiebelPropertySet(); temp.setType("" + i); if (jsonArray.get(i).isJsonObject()) child.addChild(JsonObjectToPropertySet(jsonArray.get(i).getAsJsonObject(), temp)); else { JsonObject aux = new JsonObject(); aux.add("" + i, jsonArray.get(i)); child.addChild(JsonObjectToPropertySet(aux, temp)); } } else child.setProperty("" + i, jsonArray.get(i).getAsString()); } ps.addChild(child); } else if (jsonelement.isJsonObject()) { jsonObject = jsonelement.getAsJsonObject(); child = new SiebelPropertySet(); child.setType(mapEntry.getKey().toString()); ps.addChild(JsonObjectToPropertySet(jsonObject, child)); } else { ps.setProperty(mapEntry.getKey().toString(), mapEntry.getValue().toString()); } } } return ps; }
From source file:accessControl.AuthAPIService.java
License:Open Source License
public ArrayList<Path> getAvailablePaths(String subjectInfo, boolean isCertificate) { HttpURLConnection connection = null; try {//from w w w . j av a 2 s .c om //Create connection String finalURL = authServiceURI + "?getPaths=true"; logger.debug("Sending request. URI:" + finalURL); URL url = new URL(finalURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); if (!isCertificate) connection.setRequestProperty("Cookie", subjectInfo); connection.setUseCaches(false); connection.setDoOutput(true); connection.connect(); if (isCertificate) { DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(subjectInfo); wr.flush(); wr.close(); } //Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); // or StringBuffer if not Java 5+ String line; while ((line = rd.readLine()) != null) { response.append(line); } rd.close(); String response_result = response.toString(); // If the whole tree is allowed (administrator mode) // We need this since the list of administrators is stored on AC side if (response_result.equalsIgnoreCase("true")) { ArrayList<Path> res = new ArrayList<>(); res.add(new Path("all")); return res; } else if (response_result.equalsIgnoreCase("false")) return null; // Parse the paths and return them JsonObject paths = new JsonParser().parse(response_result).getAsJsonObject(); JsonArray json_paths = paths.getAsJsonArray("paths"); ArrayList<Path> finalPaths = new ArrayList<>(json_paths.size()); logger.debug(json_paths.size() + " PATHS FOUND"); for (int i = 0; i < json_paths.size(); i++) { String pathString = json_paths.get(i).getAsString(); logger.debug(pathString); Path nextPath = new Path(pathString); finalPaths.add(nextPath); } return finalPaths; } catch (Exception e) { logger.error("During getAvailablePaths request", e); return null; } finally { if (connection != null) { connection.disconnect(); } } }
From source file:account.PurchsaeReturnRegisterDetailAccount.java
private void jbtnViewActionPerformedRoutine() { try {/*ww w . java 2 s . c o m*/ AccountAPI accountAPI = lb.getRetrofit().create(AccountAPI.class); if (ac_cd == null) { ac_cd = ""; } if (!jCheckBox1.isSelected()) { ac_cd = ""; } JsonObject call = accountAPI.PurchaseReturnRegisterAccount(jComboBox1.getSelectedIndex(), jComboBox3.getSelectedIndex(), lb.ConvertDateFormetForDB(jtxtFromDate.getText()), lb.ConvertDateFormetForDB(jtxtToDate.getText()), ac_cd).execute().body(); lb.addGlassPane(this); if (call != null) { JsonObject result = call; if (result.get("result").getAsInt() == 1) { JsonArray array = call.getAsJsonArray("data"); dtm.setRowCount(0); dtm.setRowCount(0); double cash = 0.00, bank = 0.00, card = 0.00; String tax_name = ""; for (int i = 0; i < array.size(); i++) { Vector row = new Vector(); row.add(array.get(i).getAsJsonObject().get("ref_no").getAsString()); row.add(array.get(i).getAsJsonObject().get("v_type").getAsString()); row.add(array.get(i).getAsJsonObject().get("inv_no").getAsString()); row.add(lb.ConvertDateFormetForDisplay( array.get(i).getAsJsonObject().get("v_date").getAsString())); row.add(array.get(i).getAsJsonObject().get("fname").getAsString()); row.add(array.get(i).getAsJsonObject().get("SR_ALIAS").getAsString()); cash = array.get(i).getAsJsonObject().get("CASH_AMT").getAsDouble(); bank = array.get(i).getAsJsonObject().get("BANK_AMT").getAsDouble(); card = array.get(i).getAsJsonObject().get("CARD_AMT").getAsDouble(); tax_name = array.get(i).getAsJsonObject().get("tax_name").getAsString(); row.add(array.get(i).getAsJsonObject().get("SR_NAME").getAsString()); row.add(array.get(i).getAsJsonObject().get("IMEI_NO").getAsString()); row.add(array.get(i).getAsJsonObject().get("SERAIL_NO").getAsString()); row.add(array.get(i).getAsJsonObject().get("QTY").getAsInt()); row.add(array.get(i).getAsJsonObject().get("RATE").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("BASIC_ITEM_AMT").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("tax_amt").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("add_tax_amt").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("DISC_RATE").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("MRP").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("AMT").getAsDouble()); row.add(array.get(i).getAsJsonObject().get("net_amt").getAsDouble()); row.add(tax_name); row.add(cash); row.add(bank); row.add(card); row.add(array.get(i).getAsJsonObject().get("bajaj_amt").getAsDouble()); row.add(lb.Convert2DecFmtForRs( array.get(i).getAsJsonObject().get("net_amt").getAsDouble() - cash - bank - card - array.get(i).getAsJsonObject().get("bajaj_amt").getAsDouble())); row.add(Constants.BRANCH.get(array.get(i).getAsJsonObject().get("branch_cd").getAsInt() - 1) .getBranch_name()); dtm.addRow(row); } lb.setColumnSizeForTable(jTable1, jPanel1.getWidth()); lb.removeGlassPane(PurchsaeReturnRegisterDetailAccount.this); } else { lb.removeGlassPane(PurchsaeReturnRegisterDetailAccount.this); lb.showMessageDailog(call.get("Cause").getAsString()); } } } catch (IOException ex) { Logger.getLogger(PurchsaeReturnRegisterDetailAccount.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:account.SalesRegisterCardWise.java
private void jbtnViewActionPerformedRoutine() { try {/* w w w . j a v a2 s. com*/ AccountAPI accountAPI = lb.getRetrofit().create(AccountAPI.class); JsonObject call = accountAPI.SalesRegisterCardWise(jCheckBox1.isSelected()).execute().body(); lb.addGlassPane(this); if (call != null) { JsonObject result = call; if (result.get("result").getAsInt() == 1) { JsonArray array = call.getAsJsonArray("data"); dtm.setRowCount(0); double net = 0.00; for (int i = 0; i < array.size(); i++) { try { Vector row = new Vector(); row.add(i + 1); row.add((array.get(i).getAsJsonObject().get("AC_CD").getAsString())); row.add((array.get(i).getAsJsonObject().get("FNAME") == null) ? "" : array.get(i).getAsJsonObject().get("FNAME").getAsString()); row.add((array.get(i).getAsJsonObject().get("CARD_NO").getAsString())); row.add((array.get(i).getAsJsonObject().get("MOBILE1").getAsString())); row.add(lb.Convert2DecFmtForRs( array.get(i).getAsJsonObject().get("NET_AMT").getAsDouble())); dtm.addRow(row); net += array.get(i).getAsJsonObject().get("NET_AMT").getAsDouble(); } catch (Exception e) { } } Vector row = new Vector(); row.add(" "); row.add(" "); row.add(" "); row.add(" "); row.add(" "); row.add(" "); dtm.addRow(row); row = new Vector(); row.add(""); row.add(""); row.add(""); row.add(""); row.add(""); row.add(lb.Convert2DecFmtForRs(net)); dtm.addRow(row); lb.setColumnSizeForTable(jTable1, jPanel1.getWidth()); lb.removeGlassPane(SalesRegisterCardWise.this); } else { lb.removeGlassPane(SalesRegisterCardWise.this); lb.showMessageDailog(call.get("Cause").getAsString()); } } } catch (IOException ex) { Logger.getLogger(SalesRegisterCardWise.class.getName()).log(Level.SEVERE, null, ex); } }