Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

In this page you can find the example usage for org.json JSONArray put.

Prototype

public JSONArray put(int index, Object value) throws JSONException 

Source Link

Document

Put or replace an object value in the JSONArray.

Usage

From source file:javaserver.User.java

public JSONArray getActionsToTake() {
    JSONArray retVal = new JSONArray();
    for (int i = 0; i < actions.length(); i++) {
        retVal.put(i, actions.get(i));
    }/*w  w w  .j  a v  a  2s  . co m*/
    emptyActions();
    return retVal;
}

From source file:config.Manejador.java

public boolean update(JSONObject criteria, JSONObject changes) throws JSONException, IOException {
    JSONObject js_obj = new JSONObject(this.src_archivo);
    JSONArray js_array = (JSONArray) js_obj.get(this.nom_coleccion);

    boolean retorno = false;

    JSONObject busqueda = this.Select(criteria);

    if (!busqueda.equals(this.dont_exists)) {
        Iterator it_changes = changes.keys();
        while (it_changes.hasNext()) {
            String key_c = it_changes.next().toString();
            busqueda.put(key_c, changes.get(key_c));
        }//w  ww.  j av  a2 s . com

        int idx = busqueda.getInt("idx");
        busqueda.remove("idx");

        js_obj.put(this.nom_coleccion, js_array.put(idx, busqueda));
        retorno = true;
    }

    this.src_archivo = js_obj.toString(4);
    this.saveFile();
    return retorno;
}

From source file:config.Manejador.java

public boolean updateIntoArray(JSONObject criteria, JSONObject changes) throws JSONException, IOException {
    boolean retorno = false;

    JSONObject js_obj = new JSONObject(this.src_archivo);
    JSONArray js_array = (JSONArray) js_obj.get(this.nom_coleccion);

    if (criteria.has("idx")) {
        int idx_registro = criteria.getInt("idx");
        JSONObject registro = js_array.getJSONObject(idx_registro);
        JSONArray arr_registro, arr_criteria, arr_changes;
        JSONObject obj_arr_registro, obj_arr_criteria, obj_arr_changes;

        Iterator it_changes = changes.keys();

        while (it_changes.hasNext()) {
            String key = it_changes.next().toString();

            int idx_arr_registro = 0;
            arr_registro = registro.getJSONArray(key);
            arr_criteria = criteria.getJSONArray(key);
            arr_changes = changes.getJSONArray(key);

            obj_arr_criteria = arr_criteria.getJSONObject(0);
            obj_arr_changes = arr_changes.getJSONObject(0);

            idx_arr_registro = obj_arr_criteria.getInt("idx");
            obj_arr_registro = arr_registro.getJSONObject(idx_arr_registro);

            Iterator field_changes = obj_arr_changes.keys();
            while (field_changes.hasNext()) {
                String key_change = field_changes.next().toString();
                obj_arr_registro.put(key_change, obj_arr_changes.get(key_change));
            }// ww w  .ja v a  2 s  .c om

            arr_registro.put(idx_arr_registro, obj_arr_registro);
            registro.put(key, arr_registro);

            js_obj.put(this.nom_coleccion, js_array.put(idx_registro, registro));
            retorno = true;
        }
    }

    this.src_archivo = js_obj.toString(4);
    this.saveFile();
    return retorno;
}

From source file:config.Manejador.java

public boolean deleteIntoArray(JSONObject criteria, JSONObject target) throws JSONException, IOException {
    boolean retorno = false;

    JSONObject js_obj = new JSONObject(this.src_archivo);
    JSONArray js_array = (JSONArray) js_obj.get(this.nom_coleccion);

    System.out.println(criteria.toString(4));

    if (criteria.has("idx")) {
        int idx_registro = criteria.getInt("idx");
        int idx_arr_obj = 0;

        JSONObject registro = js_array.getJSONObject(idx_registro);

        JSONArray arr_registro, arr_criteria;
        JSONObject obj_arr_criteria;/*ww  w  .  j  ava  2 s .co m*/

        Iterator it_target = target.keys();
        if (it_target.hasNext()) {
            String key = it_target.next().toString();

            arr_registro = registro.getJSONArray(key);
            arr_criteria = criteria.getJSONArray(key);

            obj_arr_criteria = arr_criteria.getJSONObject(0);

            idx_arr_obj = obj_arr_criteria.getInt("idx");

            arr_registro.remove(idx_arr_obj);
            registro.put(key, arr_registro);

            js_array.put(idx_registro, registro);
            js_obj.put(this.nom_coleccion, js_array);
            retorno = true;

            this.src_archivo = js_obj.toString(4);
            this.saveFile();
        }
    }

    return retorno;
}

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

public void _updateStats(Card card, String type, long cnt) {
    String key = type + "Today";
    long did = card.getDid();
    ArrayList<JSONObject> list = mCol.getDecks().parents(did);
    list.add(mCol.getDecks().get(did));/*from w w  w .j  a va  2  s. co  m*/
    for (JSONObject g : list) {
        try {
            JSONArray a = g.getJSONArray(key);
            // add
            a.put(1, a.getLong(1) + cnt);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        mCol.getDecks().save(g);
    }
}

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

public void extendLimits(int newc, int rev) {
    JSONObject cur = mCol.getDecks().current();
    ArrayList<JSONObject> decks = new ArrayList<JSONObject>();
    decks.add(cur);/*from w w w.ja v  a2  s .  c  o  m*/
    try {
        decks.addAll(mCol.getDecks().parents(cur.getLong("id")));
        for (long did : mCol.getDecks().children(cur.getLong("id")).values()) {
            decks.add(mCol.getDecks().get(did));
        }
        for (JSONObject g : decks) {
            // add
            JSONArray ja = g.getJSONArray("newToday");
            ja.put(1, ja.getInt(1) - newc);
            g.put("newToday", ja);
            ja = g.getJSONArray("revToday");
            ja.put(1, ja.getInt(1) - rev);
            g.put("revToday", ja);
            mCol.getDecks().save(g);
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jennifer.ui.util.scale.TimeScale.java

public Scale domain(JSONArray domain) {

    for (int i = 0, len = domain.length(); i < len; i++) {
        if (domain.get(i) instanceof Date) {
            domain.put(i, ((Date) domain.get(i)).getTime());
        } else {//from  w  ww . java  2  s  .  c om
            domain.put(i, domain.get(i));
        }
    }

    return super.domain(domain);
}

From source file:no.opentech.shoppinglist.file.JSONHandler.java

public String createJSONFromItemList(ArrayList<Item> itemList) {
    JSONArray jsonItems = new JSONArray();
    try {/* w  ww  . j a v  a  2 s.c o  m*/
        int i = 0;
        for (Item item : itemList) {
            log.debug("JSONifying " + item.getName());
            jsonItems.put(i, item.getName());
            jsonItems.put(1 + i, item.getDescription());
            jsonItems.put(2 + i, item.getUsageCounter());
            jsonItems.put(3 + i, item.getNumberInLine());
            jsonItems.put(4 + i, Utils.getTimeStamp(item.getFirstSeen()));
            jsonItems.put(5 + i, Utils.getTimeStamp(item.getLastSeen()));
            i += 6;
        }
    } catch (JSONException e) {
        return null;
    }
    return jsonItems.toString();
}