Example usage for org.json JSONObject getJSONArray

List of usage examples for org.json JSONObject getJSONArray

Introduction

In this page you can find the example usage for org.json JSONObject getJSONArray.

Prototype

public JSONArray getJSONArray(String key) throws JSONException 

Source Link

Document

Get the JSONArray value associated with a key.

Usage

From source file:de.jaetzold.philips.hue.HueBridge.java

private void parseGroups(JSONObject groupsJson) {
    final Iterator<?> keys = groupsJson.keys();
    while (keys.hasNext()) {
        Object key = keys.next();
        try {//from  ww  w. j  av  a 2s .c  om
            Integer id = Integer.parseInt((String) key);
            HueLightGroup group = groups.get(id);
            if (group == null) {
                group = new HueLightGroup(this, id);
                groups.put(id, group);
            }

            final JSONObject lightJson = groupsJson.getJSONObject((String) key);
            group.name = lightJson.getString("name");

            final JSONArray lightsArray = lightJson.getJSONArray("lights");
            for (int i = 0; i < lightsArray.length(); i++) {
                Integer lightId = Integer.parseInt(lightsArray.getString(i));
                final HueLightBulb light = getLight(lightId);
                if (light == null) {
                    //noinspection ThrowCaughtLocally
                    throw new HueCommException("Can not find light with id " + lightId);
                } else {
                    group.lights.put(lightId, light);
                }
            }

        } catch (Exception e) {
            if (e instanceof HueCommException) {
                throw e;
            } else {
                throw new HueCommException("Groups result parsing failed. Probably some unexpected format?", e);
            }
        }
    }
}

From source file:com.example.android.camera2basic.VisualRecognition.java

@Override
protected String doInBackground(Object... paths) {
    System.out.println("Performing Visual Recognition...");

    // params comes from the execute() call: params[0] is the url.
    com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition service = new com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition(
            com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition.VERSION_DATE_2016_05_20);
    service.setApiKey("26a259b7f5dc0f5c8c1cc933d8722b0e66aed5df");

    File actualImageFile = new File((String) paths[0]);
    // Library link : https://github.com/zetbaitsu/Compressor
    Bitmap compressedBitmap = Compressor.getDefault(mContext).compressToBitmap(actualImageFile);
    DirectoryPath = (String) paths[1];
    File compressedImage = bitmapToFile(compressedBitmap);
    System.out.println(/*from w ww  .j a v a2s  . co m*/
            "The size of image for VisualClassification (in kB) : " + (compressedImage.length() / 1024));
    // TODO Image size may be still greater than 1 MB !
    ClassifyImagesOptions options = new ClassifyImagesOptions.Builder().images(compressedImage).build();
    VisualClassification result = service.classify(options).execute();
    System.out.println(result);

    try {
        JSONObject obj = new JSONObject(result.toString());
        JSONObject resultarray1 = obj.getJSONArray("images").getJSONObject(0);
        classes = resultarray1.getJSONArray("classifiers").getJSONObject(0).getJSONArray("classes")
                .getJSONObject(0).getString("class");
        System.out.println("Classes : " + classes);
        //  new TextToSpeechTask().execute(classes,DirectoryPath);
    } catch (JSONException e) {
        System.out.println("Nothing Detected");
    }
    countDownLatch.countDown();
    System.out.println("Latch counted down in VisualRecongition");

    return result.toString();
}

From source file:com.jennifer.ui.chart.widget.LegendWidget.java

private JSONArray getLegendIcon(JSONObject brushObject) {
    JSONArray arr = new JSONArray();
    JSONArray data = JSONUtil.clone(brushObject.getJSONArray("target"));

    if (key != null && key.length() > 0) {
        data = chart.data();// w  ww.ja  v  a  2 s.co m
    }

    int count = data.length();

    for (int i = 0; i < count; i++) {
        String text = "";

        if (key != null && key.length() > 0) {
            text = chart.series(key).optString("text", data.getJSONObject(i).getString(key));
        } else {
            String target = data.getString(i);
            text = chart.series(target).optString("text", target);
        }

        double rectWidth = (fontWidth - 4) * text.length();
        double width = Math.min(rectWidth, fontHeight);
        double height = width;

        Transform group = root.group(new JSONObject().put("class", "legend icon"));

        Transform rect = group.rect(new JSONObject().put("x", 0).put("y", 0).put("width", width)
                .put("height", height).put("fill", chart.color(i, brushObject.optJSONArray("colors"))));

        group.text(new JSONObject().put("x", width + 4).put("y", fontHeight - 3) // 3 is top, bottom font margin
                .put("font-family", chart.theme("fontFamily")).put("font-size", chart.theme("legendFontSize"))
                .put("fill", chart.theme("legendFontColor")).put("text-anchor", "start")).textNode(text);

        arr.put(new JSONObject().put("width", width + 4 + rectWidth + (i == count - 1 ? 0 : 10))
                .put("height", height + 4).put("icon", group)

        );

    }

    return arr;
}

From source file:com.hichinaschool.flashcards.libanki.Card.java

public JSONObject template() {
    JSONObject m = model();
    try {/*ww w  .  ja v a 2s . c  om*/
        if (m.getInt("type") == Sched.MODEL_STD) {
            return m.getJSONArray("tmpls").getJSONObject(mOrd);
        } else {
            return model().getJSONArray("tmpls").getJSONObject(0);
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.chess.genesis.net.SyncClient.java

private void sync_recent(final JSONObject json) {
    try {/*w  ww  .  ja  v a 2s  . co m*/
        final JSONArray ids = json.getJSONArray("gameids");
        final ExecutorService pool = Executors.newCachedThreadPool();

        for (int i = 0, len = ids.length(); i < len; i++) {
            if (error)
                return;
            final NetworkClient nc = new NetworkClient(context, handle);
            nc.game_status(ids.getString(i));
            pool.submit(nc);

            lock++;
        }
        // Save sync time
        final PrefEdit pref = new PrefEdit(context);
        pref.putLong(R.array.pf_lastgamesync, json.getLong("time"));
        pref.commit();
    } catch (final JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.chess.genesis.net.SyncClient.java

private void sync_active(final JSONObject json) {
    try {//from   ww  w . j a v a2s.com
        final ArrayList<String> list_need = getNeedList(json.getJSONArray("gameids"));
        final ExecutorService pool = Executors.newCachedThreadPool();

        for (final String item : list_need) {
            if (error)
                return;
            final NetworkClient nc = new NetworkClient(context, handle);
            nc.game_info(item);
            pool.submit(nc);

            lock++;
        }
        // don't save time if only syncing active
        if (syncType == ACTIVE_SYNC)
            return;
        // Save sync time
        final long time = json.getLong("time");
        final PrefEdit pref = new PrefEdit(context);
        pref.putLong(R.array.pf_lastgamesync, time);
        pref.commit();
    } catch (final JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.chess.genesis.net.SyncClient.java

private void sync_archive(final JSONObject json) {
    try {//  ww  w  . ja  va 2s. co m
        final ArrayList<String> list_need = getNeedList(json.getJSONArray("gameids"));
        final ExecutorService pool = Executors.newCachedThreadPool();

        for (final String item : list_need) {
            if (error)
                return;
            final NetworkClient nc = new NetworkClient(context, handle);
            nc.game_data(item);
            pool.submit(nc);

            lock++;
        }
    } catch (final JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.chess.genesis.net.SyncClient.java

private void saveMsgs(final JSONObject data) {
    try {/*  w  w  w .  ja va 2  s.c o m*/
        final JSONArray msgs = data.getJSONArray("msglist");
        final long time = data.getLong("time");
        final GameDataDB db = new GameDataDB(context);

        for (int i = 0, len = msgs.length(); i < len; i++) {
            final JSONObject item = msgs.getJSONObject(i);
            db.insertMsg(item);
        }
        db.close();

        // Save sync time
        final PrefEdit pref = new PrefEdit(context);
        pref.putLong(R.array.pf_lastmsgsync, time);
        pref.commit();
    } catch (final JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.foxykeep.datadroidpoc.data.factory.PhoneDeleteFactory.java

public static long[] parseResult(String wsResponse) throws DataException {

    long[] deletedPhoneIdArray = null;

    try {//from  w w  w. j a v  a  2 s .  c  o m
        JSONObject parser = new JSONObject(wsResponse);
        JSONObject jsonRoot = parser.getJSONObject(JSONTag.CRUD_PHONE_DELETE_ELEM_PHONES);
        JSONArray jsonPhoneArray = jsonRoot.getJSONArray(JSONTag.CRUD_PHONE_DELETE_ELEM_PHONE);
        int size = jsonPhoneArray.length();

        deletedPhoneIdArray = new long[size];

        for (int i = 0; i < size; i++) {
            deletedPhoneIdArray[i] = jsonPhoneArray.getJSONObject(i).getLong(JSONTag.CRUD_PHONE_DELETE_ELEM_ID);
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
        throw new DataException(e);
    }

    return deletedPhoneIdArray;
}

From source file:com.lmpessoa.sonarview.core.Server.java

public List<Issue> getIssues(String projectId) throws MalformedURLException, IOException {
    int pageIndex = 1;
    int total = Integer.MAX_VALUE;
    List<Issue> result = new ArrayList<>();
    while (result.size() < total) {
        JSONObject response = readIssuesFromServer(projectId, pageIndex);
        if (total == Integer.MAX_VALUE) {
            total = response.getInt("total");
        }/*w ww  .  j a  va2s  . c om*/
        JSONArray issues = response.getJSONArray("issues");
        for (int i = 0; i < issues.length(); ++i) {
            result.add(new Issue(issues.getJSONObject(i)));
        }
        pageIndex += 1;
    }
    return result;
}