List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:jsonSploit.java
/** * @param args the command line arguments *//*from ww w . j a v a 2 s. c om*/ 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:PacketSnifferPlugin.java
License:Apache License
public void updateTable(String command) { HashMap<String, HashMap> hash = null; try {/*w w w . ja v a 2 s . co m*/ DefaultTableModel model = ((DefaultTableModel) jTable1.getModel()); Object obj = client.execute("runPlugin", new Object[] { variables.get("user"), getName(), "command=query&data=" + index }); //System.out.println("Response: "+obj.toString()); hash = (HashMap<String, HashMap>) obj; //hash = (HashMap<String, HashMap>) client.execute("runPlugin", // new Object[] { variables.get("user"), getName(), // "command=query&data=" + index }); HashMap hash2 = hash.get("data"); if (hash2.isEmpty()) { return; } index = hash2.get("id").toString(); //System.out.println("index: "+index); Object[] hash3 = (Object[]) hash2.get("packets"); //System.out.println("packets length: "+hash3.length); String protocol, sip, smac, sport, dip, dmac, dport, id = null; for (Object ob : hash3) { try { JsonElement el = new JsonParser().parse(ob.toString()); JsonObject jobject = el.getAsJsonObject(); el = jobject.get("packet_head"); jobject = el.getAsJsonObject(); JsonElement source = jobject.get("source"); jobject = source.getAsJsonObject(); source = jobject.get("ip"); sip = source.toString(); sip = sip.substring(1, sip.length() - 1); source = jobject.get("mac"); smac = source.toString(); smac = smac.substring(1, smac.length() - 1); source = jobject.get("port"); sport = source.toString(); jobject = el.getAsJsonObject(); source = jobject.get("destination"); jobject = source.getAsJsonObject(); source = jobject.get("ip"); dip = source.toString(); dip = dip.substring(1, dip.length() - 1); source = jobject.get("mac"); dmac = source.toString(); dmac = dmac.substring(1, dmac.length() - 1); source = jobject.get("port"); dport = source.toString(); jobject = el.getAsJsonObject(); source = jobject.get("protocol"); protocol = source.toString(); protocol = protocol.substring(1, protocol.length() - 1); source = jobject.get("id"); id = source.toString(); id = id.substring(1, id.length() - 1); model.addRow(new Object[] { protocol, sip, smac, sport, dip, dmac, dport, id }); } catch (Exception e) { System.out.println("Server response: " + hash.toString()); e.printStackTrace(); } } if (model.getRowCount() > length) { int delete = model.getRowCount() - length; for (int i = 0; i < delete; i++) { //System.out.println("delete"); model.removeRow(0); } } } catch (Exception e) { System.out.println("Server response: " + hash.toString()); e.printStackTrace(); } }
From source file:$.$.java
License:Apache License
private String parseArg(String arguments, ResourceOperation operation, PropertyValue value, String object) {// www.ja v a 2s . co m // parse the argument string and get the "value" parameter JsonObject args; String val = null; JsonElement jsonElem = null; Boolean passed = true; // check for parameters from the command if (arguments != null) { args = new JsonParser().parse(arguments).getAsJsonObject(); jsonElem = args.get(object); } // if the parameter is passed from the command, use it, otherwise treat parameter // as the default if (jsonElem == null || jsonElem.toString().equals("null")) { val = operation.getParameter(); passed = false; } else { val = jsonElem.toString().replace("\"", ""); } // if no value is specified by argument or parameter, take the object default from the profile if (val == null) { val = value.getDefaultValue(); passed = false; } // if a mapping translation has been specified in the profile, use it Map<String,String> mappings = operation.getMappings(); if (mappings != null && mappings.containsKey(val)) { val = mappings.get(val); passed = false; } if (!value.mask().equals(BigInteger.ZERO) && passed) { val = transform.format(value, val); } return val; }
From source file:ambari.interaction.NodeController.java
public static ArrayList<ComponentInformation> getListOfNodes() throws Exception { String url = "http://127.0.0.1:8080/api/v1/clusters/mycluster/services/HDFS/components/DATANODE?fields=host_components/HostRoles/desired_admin_state,host_components/HostRoles/state"; String responseJson = HttpURLConnectionExample.sendGet(url); System.out.print(responseJson); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse(responseJson); ArrayList<ComponentInformation> listOfCompoents = new ArrayList<ComponentInformation>(); if (jsonTree.isJsonObject()) { JsonObject jsonObject = jsonTree.getAsJsonObject(); JsonElement hostComponents = jsonObject.get("host_components"); if (hostComponents.isJsonArray()) { JsonArray hostComponentsJsonArray = hostComponents.getAsJsonArray(); System.out.println("\nStart"); System.out.println(hostComponentsJsonArray); for (int i = 0; i < hostComponentsJsonArray.size(); i++) { JsonElement hostComponentsElem = hostComponentsJsonArray.get(i); if (hostComponentsElem.isJsonObject()) { JsonObject jsonObjectElem = hostComponentsElem.getAsJsonObject(); JsonElement hostRolesElem = jsonObjectElem.get("HostRoles"); if (hostRolesElem.isJsonObject()) { JsonObject hostRolesObjs = hostRolesElem.getAsJsonObject(); JsonElement componentNameElem = hostRolesObjs.get("component_name"); JsonElement hostNameElem = hostRolesObjs.get("host_name"); JsonElement stateElem = hostRolesObjs.get("state"); listOfCompoents.add(new ComponentInformation(componentNameElem.toString(), hostNameElem.toString(), stateElem.toString())); }/*from ww w . j a v a 2s . com*/ } } } } return listOfCompoents; }
From source file:angularBeans.util.AngularBeansUtils.java
License:LGPL
public Object convertEvent(NGEvent event) throws ClassNotFoundException { JsonElement element = CommonUtils.parse(event.getData()); JsonElement data; Class javaClass;/*w w w . j a v a 2 s . c om*/ try { data = element.getAsJsonObject(); javaClass = Class.forName(event.getDataClass()); } catch (Exception e) { data = element.getAsJsonPrimitive(); if (event.getDataClass() == null) { event.setDataClass("String"); } javaClass = Class.forName("java.lang." + event.getDataClass()); } Object o; if (javaClass.equals(String.class)) { o = data.toString().substring(1, data.toString().length() - 1); } else { o = deserialise(javaClass, data); } return o; }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode bill, using storage array(with prices) and bill json and return total price. * @param billJson//from ww w . jav a 2 s .c o m * @param magazinList * @return */ public static float decodeBill(String billJson, ArrayList<Food> magazinList) { JsonElement jelement = new JsonParser().parse(billJson); JsonObject json = jelement.getAsJsonObject(); JsonElement billElement = json.get("Bill"); System.out.println("Bill JSON " + billElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder billdecoder = gson.fromJson(billElement, FoodJsonDecoder.class); return billdecoder.billPrice(magazinList); }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode recipe using the three food items from json recipe. * @param recipeJson//from w ww. ja v a2 s . c o m * @return */ public static Recipe decodeRecipe(String recipeJson) { Recipe recipe; Food[] component = new Food[3]; JsonElement jelement = new JsonParser().parse(recipeJson); JsonObject json = jelement.getAsJsonObject(); JsonElement recipeElement = json.get("Recipe"); System.out.println("Recipe JSON " + recipeElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder recipedecoder = gson.fromJson(recipeElement, FoodJsonDecoder.class); int i = 0; for (Food item : recipedecoder.foodList()) { if (item.quantity > 0) { component[i++] = item; } } recipe = new Recipe("eaten", component[0], component[1], component[2]); return recipe; }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode storage update json, return entire catalogue list * @param storageJson/*from w ww.j a v a 2 s .co m*/ * @return */ public static ArrayList<Food> decodeStorage(String storageJson) { System.out.println("Storage Json : " + storageJson); JsonElement jelement = new JsonParser().parse(storageJson); JsonObject json = jelement.getAsJsonObject(); JsonElement storageElement = json.get("Storage"); System.out.println("Storage JSON " + storageElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder recipedecoder = gson.fromJson(storageElement, FoodJsonDecoder.class); return recipedecoder.foodList(); }
From source file:ar.com.wolox.wolmo.networking.retrofit.serializer.LocalDateSerializer.java
License:Open Source License
/** * Transforms a {@link JsonElement} representing a {@link java.util.Date} into a * {@link LocalDate} class from the JodaTime library. * This is useful for receiving data over the network. * * @param element a {@link JsonElement} representing a {@link java.util.Date} * * @return returns an instance of {@link LocalDate} representing a {@link java.util.Date} *//*from w w w. j a v a 2 s. c o m*/ @Override public LocalDate deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { initFormatter(); String date = element.toString(); if (date.startsWith("\"") && date.endsWith("\"")) { date = date.substring(1, date.length() - 1); } return mDateTimeFormatter.parseLocalDate(date); }
From source file:basic.BasicExample.java
License:Open Source License
@Override public void onMessage(JsonElement json, IOAcknowledge ack) { try {/* w w w. java 2 s .c om*/ System.out.println("Server said:" + json.toString()); } catch (JsonSyntaxException e) { e.printStackTrace(); } }