List of usage examples for org.json JSONArray getDouble
public double getDouble(int index) throws JSONException
From source file:com.nginious.http.serialize.JsonDeserializer.java
/** * Deserializes property with the specified name in the specified json object into a float array. * //www. j a v a 2s . c o m * @param object the json object * @param name the property name * @return the deserialized array or <code>null</code> if property doesn't exist * @throws SerializerException if unable to deserialize value */ protected float[] deserializeFloatArray(JSONObject object, String name) throws SerializerException { try { if (object.has(name)) { JSONArray array = object.getJSONArray(name); float[] outArray = new float[array.length()]; for (int i = 0; i < array.length(); i++) { outArray[i] = (float) array.getDouble(i); } return outArray; } return null; } catch (JSONException e) { throw new SerializerException("Can't deserialize float array property " + name, e); } }
From source file:twitter4j.internal.json.InternalJSONFactoryImpl.java
static GeoLocation[][] coordinatesAsGeoLocationArray(final JSONArray coordinates) throws TwitterException { try {// ww w. j a v a 2 s . com final GeoLocation[][] boundingBox = new GeoLocation[coordinates.length()][]; for (int i = 0; i < coordinates.length(); i++) { final JSONArray array = coordinates.getJSONArray(i); boundingBox[i] = new GeoLocation[array.length()]; for (int j = 0; j < array.length(); j++) { final JSONArray coordinate = array.getJSONArray(j); boundingBox[i][j] = new GeoLocation(coordinate.getDouble(1), coordinate.getDouble(0)); } } return boundingBox; } catch (final JSONException jsone) { throw new TwitterException(jsone); } }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.detect.DetectStripTask.java
private void warp(@NonNull Mat labImg, int i) throws IOException, JSONException { String jsonInfo = FileUtil.readFromInternalStorage(context, Constant.INFO + i); if (jsonInfo == null) { throw new IOException("no finder pattern info"); }//from w w w .ja v a 2 s. c om JSONObject jsonObject = new JSONObject(jsonInfo); JSONArray tl = jsonObject.getJSONArray(Constant.TOP_LEFT); JSONArray tr = jsonObject.getJSONArray(Constant.TOP_RIGHT); JSONArray bl = jsonObject.getJSONArray(Constant.BOTTOM_LEFT); JSONArray br = jsonObject.getJSONArray(Constant.BOTTOM_RIGHT); double[] topLeft = new double[] { tl.getDouble(0), tl.getDouble(1) }; double[] topRight = new double[] { tr.getDouble(0), tr.getDouble(1) }; double[] bottomLeft = new double[] { bl.getDouble(0), bl.getDouble(1) }; double[] bottomRight = new double[] { br.getDouble(0), br.getDouble(1) }; warpMat = OpenCVUtil.perspectiveTransform(topLeft, topRight, bottomLeft, bottomRight, labImg); }
From source file:com.hichinaschool.flashcards.libanki.Sched.java
private int _delayForGrade(JSONObject conf, int left) { left = left % 1000;//ww w . jav a 2 s .co m try { double delay; JSONArray ja = conf.getJSONArray("delays"); int len = ja.length(); try { delay = ja.getDouble(len - left); } catch (JSONException e) { if (conf.getJSONArray("delays").length() > 0) { delay = conf.getJSONArray("delays").getDouble(0); } else { // user deleted final step; use dummy value delay = 1.0; } } return (int) (delay * 60.0); } catch (JSONException e) { throw new RuntimeException(e); } }
From source file:com.hichinaschool.flashcards.libanki.Sched.java
private int _leftToday(JSONArray delays, int left, long now) { if (now == 0) { now = Utils.intNow();/* ww w.j av a 2s .co m*/ } int ok = 0; int offset = Math.min(left, delays.length()); for (int i = 0; i < offset; i++) { try { now += (int) (delays.getDouble(delays.length() - offset + i) * 60.0); } catch (JSONException e) { throw new RuntimeException(e); } if (now > mDayCutoff) { break; } ok = i; } return ok + 1; }
From source file:mr.robotto.engine.loader.components.skeleton.MrBoneLoader.java
private void loadRotation(MrQuaternion q, JSONArray rot) throws JSONException { int index = 0; q.w = (float) rot.getDouble(index); index++;/* w w w . ja va2 s.c o m*/ q.x = (float) rot.getDouble(index); index++; q.y = (float) rot.getDouble(index); index++; q.z = (float) rot.getDouble(index); index++; }
From source file:tectonicus.blockTypes.BlockRegistry.java
public BlockModel loadModel(String modelPath, ZipStack zips, Map<String, String> textureMap) throws Exception { ZipStackEntry modelFile = zips.getEntry("assets/minecraft/models/" + modelPath + ".json"); JSONObject obj = new JSONObject(FileUtils.loadJSON(modelFile.getInputStream())); String parent = ""; if (obj.has("parent")) // Get texture information and then load parent file {// w w w .j a v a 2 s . c o m parent = obj.getString("parent"); return loadModel(parent, zips, populateTextureMap(textureMap, obj.getJSONObject("textures"))); } else //Load all elements { Map<String, String> combineMap = new HashMap<>(textureMap); if (obj.has("textures")) { combineMap.putAll(populateTextureMap(textureMap, obj.getJSONObject("textures"))); } List<BlockElement> elementsList = new ArrayList<>(); boolean ao = true; if (obj.has("ambientocclusion")) ao = false; JSONArray elements = obj.getJSONArray("elements"); for (int i = 0; i < elements.length(); i++) { Map<String, ElementFace> elementFaces = new HashMap<>(); JSONObject element = elements.getJSONObject(i); JSONArray from = element.getJSONArray("from"); Vector3f fromVector = new Vector3f((float) from.getDouble(0), (float) from.getDouble(1), (float) from.getDouble(2)); JSONArray to = element.getJSONArray("to"); Vector3f toVector = new Vector3f((float) to.getDouble(0), (float) to.getDouble(1), (float) to.getDouble(2)); Vector3f rotationOrigin = new Vector3f(8.0f, 8.0f, 8.0f); String rotationAxis = "y"; float rotationAngle = 0; boolean rotationScale = false; if (element.has("rotation")) { JSONObject rot = element.getJSONObject("rotation"); JSONArray rotOrigin = rot.getJSONArray("origin"); rotationOrigin = new Vector3f((float) rotOrigin.getDouble(0), (float) rotOrigin.getDouble(1), (float) rotOrigin.getDouble(2)); rotationAxis = rot.getString("axis"); rotationAngle = (float) rot.getDouble("angle"); if (element.has("rescale")) rotationScale = true; } boolean shaded = true; if (element.has("shade")) shaded = false; JSONObject faces = element.getJSONObject("faces"); Iterator<?> keys = faces.keys(); while (keys.hasNext()) { String key = (String) keys.next(); JSONObject face = (JSONObject) faces.get(key); float u0 = fromVector.x(); float v0 = fromVector.y(); float u1 = toVector.x(); float v1 = toVector.y(); int rotation = 0; if (face.has("rotation")) rotation = face.getInt("rotation"); //System.out.println("u0="+u0+" v0="+v0+" u1="+u1+" v1="+v1); // TODO: Need to test more texture packs SubTexture subTexture = new SubTexture(null, u0 * (1.0f / 16.0f), v0 * (1.0f / 16.0f), u1 * (1.0f / 16.0f), v1 * (1.0f / 16.0f)); StringBuilder tex = new StringBuilder(face.getString("texture")); if (tex.charAt(0) == '#') { String texture = tex.deleteCharAt(0).toString(); SubTexture te = texturePack .findTexture(StringUtils.removeStart(combineMap.get(texture), "blocks/") + ".png"); final float texHeight = te.texture.getHeight(); final float texWidth = te.texture.getWidth(); final int numTiles = te.texture.getHeight() / te.texture.getWidth(); u0 = fromVector.x() / texWidth; v0 = fromVector.y() / texWidth; u1 = toVector.x() / texWidth; v1 = toVector.y() / texWidth; if (face.has("uv")) { //System.out.println("Before: u0="+u0+" v0="+v0+" u1="+u1+" v1="+v1); JSONArray uv = face.getJSONArray("uv"); u0 = (float) (uv.getDouble(0) / 16.0f); v0 = (float) (uv.getDouble(1) / 16.0f) / numTiles; u1 = (float) (uv.getDouble(2) / 16.0f); v1 = (float) (uv.getDouble(3) / 16.0f) / numTiles; } System.out.println(texWidth + " x " + texHeight); int frame = 1; if (numTiles > 1) { Random rand = new Random(); frame = rand.nextInt(numTiles) + 1; } subTexture = new SubTexture(te.texture, u0, v0 + (float) (frame - 1) * (texWidth / texHeight), u1, v1 + (float) (frame - 1) * (texWidth / texHeight)); //subTexture = new SubTexture(test, u0, v0, u1, v1); //System.out.println("u0="+subTexture.u0+" v0="+subTexture.v0+" u1="+subTexture.u1+" v1="+subTexture.v1); } boolean cullFace = false; if (face.has("cullface")) cullFace = true; boolean tintIndex = false; if (face.has("tintindex")) tintIndex = true; ElementFace ef = new ElementFace(subTexture, cullFace, rotation, tintIndex); elementFaces.put(key, ef); } BlockElement be = new BlockElement(fromVector, toVector, rotationOrigin, rotationAxis, rotationAngle, rotationScale, shaded, elementFaces); elementsList.add(be); } return new BlockModel(modelPath, ao, elementsList); } }
From source file:org.openstreetmap.gui.persistence.JSONPersistence.java
private static MarkerData extractMarker(JSONObject pPoint) { MarkerData lReturn = new MarkerData(); JSONArray coordinates = pPoint.getJSONArray("coordinates"); lReturn.aLongitude = coordinates.getDouble(0); lReturn.aLatitude = coordinates.getDouble(1); lReturn.aName = pPoint.getJSONObject("properties").getString("name"); lReturn.aDescription = pPoint.getJSONObject("properties").getString("description"); return lReturn; }
From source file:org.openstreetmap.gui.persistence.JSONPersistence.java
private static MarkerData extractFeature(JSONObject pFeature) { MarkerData lReturn = new MarkerData(); // if the feature is a waypoint if (pFeature.has("properties") && pFeature.getJSONObject("properties").has("gpxtype") && pFeature.getJSONObject("properties").getString("gpxtype").equals("wptType") && pFeature.has("geometry")) { try {//from w w w . ja va 2 s .c om JSONArray coordinates = pFeature.getJSONObject("geometry").getJSONArray("coordinates"); lReturn.aLongitude = coordinates.getDouble(0); lReturn.aLatitude = coordinates.getDouble(1); } catch (Exception e) { System.out .println("JSONPersistence::extractFeature( " + pFeature + " ) - ERROR: " + e.getMessage()); } } if (pFeature.has("properties")) { lReturn.aName = pFeature.getJSONObject("properties").getString("name"); } return lReturn; }
From source file:entities.JsonParser.java
public static Device createDeviceFromJson(JSONObject json) throws JSONException { if (json == null) throw new JSONException("Invalid input json"); String id = (String) json.get("ID"); String name = (String) json.get("NAME"); String role = (String) json.get("ROLE"); double lat = json.getDouble("LATITUDE"); double lon = json.getDouble("LONGITUDE"); double lux = json.getDouble("LUMINOSITY"); double temperature = json.getDouble("TEMPERATURE"); double[] acceleration = new double[3]; JSONArray jArr = json.getJSONArray("ACCELERATION"); if (jArr.length() != 3) { throw new JSONException("Invalid acceleration length"); }//w w w . j a va 2s .co m acceleration[0] = jArr.getDouble(0); acceleration[1] = jArr.getDouble(1); acceleration[2] = jArr.getDouble(2); return new Device(id, name, role, lat, lon, lux, temperature, acceleration); }