List of usage examples for com.google.gson JsonArray iterator
public Iterator<JsonElement> iterator()
From source file:jsonSploit.java
/** * @param args the command line arguments *//* w w w .j a v a 2 s .c o m*/ public static void main(String[] args) { // TODO code application logic here Gson gson = new Gson(); System.out.println("OBTENER SOLO UN ARRAY DE CADENA JSON"); String myURL = "http://192.168.5.44/app_dev.php/cus/getaccount/50241109321.json"; System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) { urlConn.setReadTimeout(60 * 1000); } if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Exception while calling URL:" + myURL, e); } String jsonResult = sb.toString(); System.out.println(sb.toString()); System.out.println("\n\n--------------------JSON OBJECT DISPLAY----------------------------\n\n"); // convertimos el string a json Object JSONObject jsonObj = new JSONObject(jsonResult); // System.out.println("LA CADENA JSON CONVERTIDA EN OBJETO ES>"); System.out.println(jsonObj.toString()); System.out .println("\n\n--------------------- OBTENER ARRAYS DEL OBJETO JSON---------------------------\n\n"); JSONArray jsonArray = new JSONArray(); jsonArray.put(jsonObj); gson = new Gson(); Iterator x = jsonObj.keys(); while (x.hasNext()) { String key = (String) x.next(); jsonArray.put(jsonObj.get(key)); System.out.println(key); } System.out .println("\n\n--------------------- OBTENER ARRAYS DEL OBJETO JSON---------------------------\n\n"); JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject) jsonParser.parse(jsonResult); JsonArray jsonArr = jo.getAsJsonArray("data"); Gson googleJson = new Gson(); ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class); System.out.println("Listas existentes en data : " + jsonObjList.size()); System.out.println("los elementos de la lista son : " + jsonObjList.toString()); String dataResult = jsonObjList.toString(); System.out.println("\n\n--------------------- EL OBJETO JSON ARRAY---------------------------\n\n"); /// jsonArr.remove(1); String elemento1 = null; Iterator<JsonElement> nombreIterator = jsonArr.iterator(); JsonElement elemento = null; while (nombreIterator.hasNext()) { elemento = nombreIterator.next(); System.out.print(elemento + " \n"); elemento1 = elemento.toString(); System.out.println("El elemento 1 es " + elemento1); } cData data = gson.fromJson(elemento1, cData.class); // Account account = gson.fromJson(jo, Account.class); if (data.getAccount().getFirst_name() == null) { System.out.println("Error"); } else { System.out.print(data.getAccount().getFirst_name()); } // System.out.println(data.acount.getNumber()); System.out.println("\n\n--------------------- OBTENEMOS EL OBJETO ACCOUNT---------------------------\n\n"); //JsonObject jObject = (JsonObject) jsonParser.parse(elemento1); // System.out.println(jObject.getAsJsonPrimitive("number")); }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java
License:Open Source License
private static ModuleDTO parseModuleJSON(JsonObject jsonModule) { UUID id = UUID.fromString(jsonModule.get("id").getAsString()); String type = jsonModule.get("type").getAsString(); UUID[] next = null, prev = null; Map<String, String> properties = new HashMap<String, String>(); if (jsonModule.has("next")) { if (jsonModule.get("next").isJsonArray()) { JsonArray jsonNext = jsonModule.get("next").getAsJsonArray(); next = new UUID[jsonNext.size()]; int i = 0; Iterator<JsonElement> it = jsonNext.iterator(); while (it.hasNext()) { JsonElement e = it.next(); next[i++] = UUID.fromString(e.getAsString()); }//from ww w . ja va 2s . c o m } else { next = new UUID[1]; next[0] = UUID.fromString(jsonModule.get("next").getAsString()); } } if (jsonModule.has("prev")) { if (jsonModule.get("prev").isJsonArray()) { JsonArray jsonPrev = jsonModule.get("prev").getAsJsonArray(); prev = new UUID[jsonPrev.size()]; int i = 0; Iterator<JsonElement> it = jsonPrev.iterator(); while (it.hasNext()) { JsonElement e = it.next(); prev[i++] = UUID.fromString(e.getAsString()); } } else { prev = new UUID[1]; prev[0] = UUID.fromString(jsonModule.get("prev").getAsString()); } } // TODO this uses the old model where properties where just stored as flatmap for (Entry<String, JsonElement> property : jsonModule.entrySet()) { String key = property.getKey(); if (key.equals("id") || key.equals("type") || key.equals("prev") || key.equals("next")) { continue; // this is only for module-specific properties } properties.put(property.getKey(), property.getValue().getAsString()); } // TODO evolve to a separate "properties" item if (jsonModule.has("properties")) { JsonObject jsonProperties = jsonModule.get("properties").getAsJsonObject(); for (Entry<String, JsonElement> jsonProperty : jsonProperties.entrySet()) { String key = jsonProperty.getKey(); String value = jsonProperty.getValue().getAsString(); properties.put(key, value); } } ModuleDTO dto = new ModuleDTO(id, type, next, prev, properties); return dto; }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public void beginArray() throws IOException { expect(JsonToken.BEGIN_ARRAY);/*from w w w. j ava 2s. c o m*/ JsonArray array = ((JsonElement) peekStack()).getAsJsonArray(); stack.add(array.iterator()); }
From source file:br.com.bean.Utilitarios.CollectionDeserializer.java
/** * @param serializedData/*from w ww . j ava 2 s . com*/ * @param type * @return */ @SuppressWarnings("unchecked") public <T> ArrayList<T> parseAsArrayList(JsonElement json, T type) { ArrayList<T> newArray = new ArrayList<T>(); Gson gson = new Gson(); JsonArray array = json.getAsJsonArray(); Iterator<JsonElement> iterator = array.iterator(); while (iterator.hasNext()) { JsonElement json2 = (JsonElement) iterator.next(); T object = (T) gson.fromJson(json2, (Class<?>) type); newArray.add(object); } return newArray; }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonArraySearchToJsonArrayFlattened(JsonArray jsonArray, JsonArray jsonArrayFlattened) { if (jsonArray == null || jsonArrayFlattened == null) { throw new IllegalArgumentException("JsonArray and/or JsonArrayFlattened parameter(s) must not be null"); }/* w ww . j a v a 2s . com*/ Iterator<JsonElement> iterator = jsonArray.iterator(); while (iterator.hasNext()) { JsonElement value = iterator.next(); if (value.isJsonObject()) { // Creates new flattened JsonObject instance. JsonObject jsonObjectFlattened = new JsonObject(); // Iterates recursively in JsonObject (value), checking content and populating the flattened JsonObject instance. deepJsonObjectSearchToJsonObjectFlattened((JsonObject) value, jsonObjectFlattened); // Adds the flattened JsonObject instance in the flattened JsonArray instance. jsonArrayFlattened.add(jsonObjectFlattened); } else if (value.isJsonArray()) { deepJsonArraySearchToJsonArrayFlattened((JsonArray) value, jsonArrayFlattened); } else { jsonArrayFlattened.add(value); } } }
From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java
License:Apache License
public List<String> findVpcIdsNameMatchingRegex(String regex) throws InterruptedException { List<String> result = new ArrayList<String>(); JsonArray jr = listVpcsJson(); if (jr == null) return result; Iterator<JsonElement> jvii = jr.iterator(); while (jvii.hasNext()) { JsonObject jvo = jvii.next().getAsJsonObject(); String name = jvo.get("name").getAsString(); if (name != null && name.matches(regex)) result.add(jvo.get("id").getAsString()); }/* www . j a v a 2 s . co m*/ LOG.debug("VPC's matching {}: {}, ", regex, result); return result; }
From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java
License:Apache License
public String findVpcIdWithCidr(String cidr) { JsonArray jr = listVpcsJson(); if (jr == null) return null; Iterator<JsonElement> jvii = jr.iterator(); List<String> cidrs = new ArrayList<String>(); while (jvii.hasNext()) { JsonObject jvo = jvii.next().getAsJsonObject(); String cidrV = jvo.get("cidr").getAsString(); if (cidrV != null && cidrV.equals(cidr)) { String vpcId = jvo.get("id").getAsString(); LOG.debug("found vpcId {} matching CIDR {}", vpcId, cidr); return vpcId; }//from ww w . jav a 2 s .c om cidrs.add(cidrV); } LOG.debug("Found VPC's with CIDR's {} but not {}", cidrs, cidr); return null; }
From source file:ca.uhn.fhir.jpa.util.jsonpatch.CopyOperation.java
License:Apache License
@Override public JsonElement apply(JsonElement original) { JsonElement result = duplicate(original); JsonElement item = path.head().navigate(result); JsonElement data = mySourcePath.head().navigate(original); if (item.isJsonObject()) { item.getAsJsonObject().add(path.tail(), data); } else if (item.isJsonArray()) { JsonArray array = item.getAsJsonArray(); int index = (path.tail().equals("-")) ? array.size() : Integer.valueOf(path.tail()); List<JsonElement> temp = new ArrayList<JsonElement>(); Iterator<JsonElement> iter = array.iterator(); while (iter.hasNext()) { JsonElement stuff = iter.next(); iter.remove();//from w w w. j a v a 2 s .c o m temp.add(stuff); } temp.add(index, data); for (JsonElement stuff : temp) { array.add(stuff); } } return result; }
From source file:ch.jamiete.hilda.moderatortools.ModeratorToolsPlugin.java
License:Apache License
@Override public void onEnable() { this.getHilda().getCommandManager().registerChannelCommand(new ArchiveCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new BlankRoleCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new CheckRolesCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new ClearCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new FlowCommand(this.getHilda(), this)); this.getHilda().getCommandManager().registerChannelCommand(new IgnoreCommand(this.getHilda(), this)); this.getHilda().getCommandManager().registerChannelCommand(new MuteCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new MuteListCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new PurgeCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new ServerinfoCommand(this.getHilda())); this.getHilda().getCommandManager().registerChannelCommand(new UserinfoCommand(this.getHilda())); this.getHilda().getBot().addEventListener(new AnnouncementsListener()); this.getHilda().getBot().addEventListener(new FlowListener(this.getHilda(), this)); final long first = Util.getNextMidnightInMillis("UTC") - System.currentTimeMillis(); this.getHilda().getExecutor().scheduleAtFixedRate(new ChannelDeletionOverseerTask(this.getHilda()), first, 86400000, TimeUnit.MILLISECONDS); // At midnight then every 24 hours Hilda.getLogger().info("Purging channel messages in " + Util.getFriendlyTime(first)); for (final Guild guild : this.getHilda().getBot().getGuilds()) { final Configuration cfg = this.getHilda().getConfigurationManager().getConfiguration(this, "ignore-" + guild.getId()); final JsonArray array = cfg.get().getAsJsonArray("channels"); if (array != null) { final Iterator<JsonElement> iterator = array.iterator(); while (iterator.hasNext()) { this.getHilda().getCommandManager().addIgnoredChannel(iterator.next().getAsString()); }/* w w w . j a v a 2 s. c o m*/ Hilda.getLogger().info("Ignored " + array.size() + " channels in " + guild.getName()); } this.updater.check(guild); } }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
public static List<ASTParagraph> parseZeppelinExport(String file) throws FileNotFoundException, IOException { final List<ASTParagraph> res = new ArrayList<>(); try (final FileReader rd = new FileReader(file);) { final JsonObject notebook = new JsonParser().parse(rd).getAsJsonObject(); final JsonArray paragraphs = notebook.getAsJsonArray("paragraphs"); final Iterator<JsonElement> it = paragraphs.iterator(); while (it.hasNext()) { final JsonObject p = it.next().getAsJsonObject(); final JsonPrimitive textOpt = p.getAsJsonPrimitive("text"); if (textOpt != null) { final String text = textOpt.getAsString(); if (text.startsWith("%cl")) { final String content = text.substring(3); final List<ASTElement> elements = parse(content); res.add(new ASTParagraph(elements)); } else { res.add(new ASTParagraph(text)); }//from ww w. j a v a 2s.co m } } } return res; }