List of usage examples for org.json.simple JSONValue parse
public static Object parse(String s)
From source file:Controladora.ConexionAPI.java
public List<Usuario> sendGetListaUsuarios(String token) { httpClient = null; // Objeto a travs del cual realizamos las peticiones request = null; // Objeto para realizar las peticiines HTTP GET o POST status = 0; // Cdigo de la respuesta HTTP reader = null; // Se usa para leer la respuesta a la peticin line = null; // Se usa para leer cada una de las lineas de texto de la respuesta List<Usuario> listaauxiliar = new ArrayList<Usuario>(); Usuario usuario = new Usuario(); // Instanciamos el objeto httpClient = new HttpClient(); // Invocamos por GET String url = "http://localhost:8090/api/usuarios/"; request = new GetMethod(url); // Aadimos los parmetros que deseemos a la peticin request.setRequestHeader("token", token); try {/*from ww w.j av a 2 s . c o m*/ // Leemos el cdigo de la respuesta HTTP que nos devuelve el servidor status = httpClient.executeMethod(request); // Vemos si la peticin se ha realizado satisfactoriamente if (status != HttpStatus.SC_OK) { System.out.println("Error\t" + request.getStatusCode() + "\t" + request.getStatusText() + "\t" + request.getStatusLine()); } else { // Leemos el contenido de la respuesta y realizamos el tratamiento de la misma. // En nuestro caso, simplemente mostramos el resultado por la salida estndar reader = new BufferedReader( new InputStreamReader(request.getResponseBodyAsStream(), request.getResponseCharSet())); JSONParser parser = new JSONParser(); JSONValue jsonvalue = new JSONValue(); String tkn = null; System.out.println("entro AL WHILE"); listaauxiliar = null; line = reader.readLine(); while (line != null) { System.out.println(line); System.out.println("uno"); // JSONObject jso = (JSONObject) parser.parse(line); JSONArray jo = (JSONArray) jsonvalue.parse(line); //(JSONArray) jso.get(""); System.out.println("antes de iterar"); Iterator<String> iterator = jo.iterator(); System.out.println("depues de iterar"); while (iterator.hasNext()) { System.out.println("Entro al while iterador"); System.out.println(iterator.next()); } System.out.println("SALIO al while iterador"); /*usuario.setMail((String) jo.get("mail")); usuario.setNombre((String) jo.get("nombre")); usuario.setUsuario((String) jo.get("usuario")); listaauxiliar.add(usuario); System.out.println((String) jo.get("mail")); System.out.println((String) jo.get("nombre")); System.out.println((String) jo.get("usuario"));*/ line = reader.readLine(); } } } catch (Exception ex) { System.err.println("Error\t: " + ex.getMessage()); ex.printStackTrace(); } finally { // Liberamos la conexin. (Tambin libera los stream asociados) request.releaseConnection(); } return listaauxiliar; }
From source file:fr.ribesg.bukkit.ncore.updater.FileDescription.java
public static SortedMap<String, FileDescription> parse(final String jsonString) { final SortedMap<String, FileDescription> result = new TreeMap<>(new Comparator<String>() { @Override// w ww.j a v a 2 s . c o m public int compare(final String a, final String b) { return -a.compareTo(b); } }); final JSONArray array = (JSONArray) JSONValue.parse(jsonString); for (final Object o : array.toArray()) { final JSONObject object = (JSONObject) o; final String fileName = (String) object.get(FILENAME_KEY); final String version = VersionUtil.getVersion((String) object.get(VERSION_KEY)); final String bukkitVersion = (String) object.get(BUKKIT_VERSION_KEY); final String type = (String) object.get(TYPE_KEY); final String link = (String) object.get(DOWNLOAD_URL_KEY); final FileDescription fileDescription = new FileDescription(fileName, version, link, type, bukkitVersion); if (VersionUtil.isRelease(version)) { result.put(version, fileDescription); } } return result; }
From source file:com.sag.tn.storm.stormmaven.main.ConfigReader.java
private ConfigReader() { InputStream is = getClass().getResourceAsStream("/config.json"); this.configJsonObject = (JSONObject) JSONValue.parse(new InputStreamReader(is)); }
From source file:com.speedment.examples.social.JSONImage.java
public static List<JSONImage> parseFrom(String json) { final JSONObject container = (JSONObject) JSONValue.parse(json); final JSONArray array = (JSONArray) container.get("images"); final List<JSONImage> images = new ArrayList<>(); array.stream().forEach(o -> {//from w ww .j a va 2s . co m final JSONObject obj = (JSONObject) o; final JSONImage img = new JSONImage(); final long time = Long.parseLong(obj.get("uploaded").toString()); final LocalDateTime ldt = LocalDateTime.ofEpochSecond(time / 1000L, (int) (time % 1000) * 1000, ZoneOffset.UTC); img.title = obj.get("title").toString(); img.description = obj.get("description").toString(); img.uploaded = ldt; img.uploader = JSONUser.parse((JSONObject) obj.get("uploader")); img.image = fromBase64(obj.get("img_data").toString()); images.add(img); }); Collections.sort(images, Comparator.reverseOrder()); return images; }
From source file:mn.EngineForge.module.general.java
public JSONArray decodeJSON() throws IOException { String FILE = readFile(this.path, this.charset); Object obj = JSONValue.parse(FILE); JSONArray data = (JSONArray) obj;/*from ww w.j a v a2s.c om*/ return data; }
From source file:com.speedment.examples.social.JSONUser.java
public static JSONUser parseOne(String json) { return parse((JSONObject) JSONValue.parse(json)); }
From source file:myproject.MyServer.java
public static void Add(HttpServletRequest request, HttpServletResponse response) throws IOException { try {//from w w w. j av a 2s . c o m String jsonString = IOUtils.toString(request.getInputStream()); JSONObject json = (JSONObject) JSONValue.parse(jsonString); String student_name = (String) json.get("student_name"); Long regno = (Long) json.get("regno"); Double cgpa = (Double) json.get("cgpa"); String query = String.format( "INSERT INTO student " + "(student_name, regno, cgpa) " + "VALUES('%s',%d,%f)", JSONValue.escape(student_name), regno, cgpa); database.runUpdate(query); String result = database.getStudent(regno); response.getWriter().write(result); } catch (Exception ex) { JSONObject output = new JSONObject(); output.put("error", "Connection failed: " + ex.getMessage()); response.getWriter().write(JSONValue.toJSONString(output)); } }
From source file:br.ufrgs.ufrgsmapas.network.LocationParser.java
public static void parseBuildings(SparseArray<BuildingVo> mTempBuilding) { // Read JSON File Connection con = HttpConnection.connect(URL); con.method(Connection.Method.POST).ignoreContentType(true); Connection.Response resp; try {// w w w .j a va 2 s . c om resp = con.execute(); } catch (IOException e) { if (DebugUtils.DEBUG) Log.e(TAG, "Error fetching positions: " + e); return; } String jsonDoc = resp.body(); // Parse JSON to find each element JSONObject jsonStartObject = (JSONObject) JSONValue.parse(jsonDoc); // Get the array with objects representing each building JSONArray jsonBuildings = (JSONArray) jsonStartObject.get("features"); int buildingsListSize = ((List) jsonBuildings).size(); // Iterate through buildings for (int i = 0; i < buildingsListSize; i++) { // Get buildingVo object JSONObject jsonBuilding = (JSONObject) jsonBuildings.get(i); // Get buildingVo id int id = ((Number) jsonBuilding.get("id")).intValue(); // Get coordinates array JSONObject jsonGeometry = (JSONObject) jsonBuilding.get("geometry"); JSONArray jsonCoordinates = (JSONArray) ((JSONArray) ((JSONArray) jsonGeometry.get("coordinates")) .get(0)).get(0); // Get latitudes and longitudes List coordList = jsonCoordinates; int coordListSize = coordList.size(); double[] latitude = new double[coordListSize]; double[] longitude = new double[coordListSize]; for (int j = 0; j < coordListSize; j++) { latitude[j] = (double) ((List) coordList.get(j)).get(1); longitude[j] = (double) ((List) coordList.get(j)).get(0); } // Create buildingVo object and add to list - store only the center of the position double[] center = centroid(latitude, longitude); BuildingVo buildingVo = new BuildingVo(id, center[0], center[1]); mTempBuilding.append(id, buildingVo); } }
From source file:at.ac.tuwien.dsg.smartcom.integration.JSONConverter.java
public static CollectiveInfo getCollectiveInfo(Identifier collective, String content) throws NoSuchCollectiveException { CollectiveInfo info = new CollectiveInfo(); info.setId(collective);/*from w w w . j av a2 s . c om*/ info.setDeliveryPolicy(DeliveryPolicy.Collective.TO_ALL_MEMBERS); //fake value final List<Identifier> peerIDs = new ArrayList<>(); info.setPeers(peerIDs); JSONObject jsonOutput = (JSONObject) JSONValue.parse(content); String[] strings = parseResults(jsonOutput); if (strings == null) { throw new NoSuchCollectiveException(); } for (String string : strings) { peerIDs.add(Identifier.peer(string)); } return info; }
From source file:ch.simas.jtoggl.Tag.java
public Tag(String jsonString) { JSONObject object = (JSONObject) JSONValue.parse(jsonString); this.id = (Long) object.get("id"); this.name = (String) object.get("name"); JSONObject workspaceObject = (JSONObject) object.get("workspace"); if (workspaceObject != null) { this.workspace = new Workspace(workspaceObject.toJSONString()); }/*from w ww. j a va2 s. c o m*/ }