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(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

/**
 * Decks ********************************************************************
 *//*w w  w  . j a va2s.co  m*/

private JSONArray getDecks() {
    JSONArray result = new JSONArray();
    try {
        if (mCol.getServer()) {
            JSONArray decks = new JSONArray();
            for (JSONObject g : mCol.getDecks().all()) {
                if (g.getInt("usn") >= mMinUsn) {
                    decks.put(g);
                }
            }
            JSONArray dconfs = new JSONArray();
            for (JSONObject g : mCol.getDecks().allConf()) {
                if (g.getInt("usn") >= mMinUsn) {
                    dconfs.put(g);
                }
            }
            result.put(decks);
            result.put(dconfs);
        } else {
            JSONArray decks = new JSONArray();
            for (JSONObject g : mCol.getDecks().all()) {
                if (g.getInt("usn") == -1) {
                    g.put("usn", mMaxUsn);
                    decks.put(g);
                }
            }
            JSONArray dconfs = new JSONArray();
            for (JSONObject g : mCol.getDecks().allConf()) {
                if (g.getInt("usn") == -1) {
                    g.put("usn", mMaxUsn);
                    dconfs.put(g);
                }
            }
            mCol.getDecks().save();
            result.put(decks);
            result.put(dconfs);
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

/**
 * Tags ********************************************************************
 *///from www  .j av  a2s .  c  o m

private JSONArray getTags() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        for (Map.Entry<String, Integer> t : mCol.getTags().allItems().entrySet()) {
            if (t.getValue() >= mMinUsn) {
                result.put(t.getKey());
            }
        }
    } else {
        for (Map.Entry<String, Integer> t : mCol.getTags().allItems().entrySet()) {
            if (t.getValue() == -1) {
                String tag = t.getKey();
                mCol.getTags().allItems().put(tag, mMaxUsn);
                result.put(tag);
            }
        }
        mCol.getTags().save();
    }
    return result;
}

From source file:com.basetechnology.s0.agentserver.field.MultiChoiceField.java

public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("type", "multi_choice");
    if (symbol.name != null)
        json.put("name", symbol.name);
    if (label != null)
        json.put("label", label);
    if (description != null)
        json.put("description", description);
    if (defaultValue != null)
        json.put("default_value", defaultValue);
    JSONArray choicesJson = new JSONArray();
    if (choices != null)
        for (String choice : choices)
            choicesJson.put(choice);
    json.put("choices", choicesJson);
    if (nominalWidth != 0)
        json.put("nominal_width", nominalWidth);
    if (compute != null)
        json.put("compute", compute);
    return json;/*from w  w  w  . j  a  va 2s  .c  o  m*/
}

From source file:io.github.minime89.passbeam.keyboard.Scancodes.java

public JSONObject dump() throws JSONException {
    JSONObject obj = new JSONObject();

    JSONArray scancodesArr = new JSONArray();
    for (Scancode scancode : scancodes) {
        scancodesArr.put(scancode.dump());
    }//from   w w  w.  j  ava2s. c o m
    obj.put("scancodes", scancodesArr);

    return obj;
}

From source file:com.example.protocol.STATUSES.java

public JSONObject toJson() throws JSONException {
    JSONObject localItemObject = new JSONObject();
    JSONArray itemJSONArray = new JSONArray();
    localItemObject.put("comments_count", comments_count);
    localItemObject.put("text", text);
    localItemObject.put("in_reply_to_screen_name", in_reply_to_screen_name);
    localItemObject.put("truncated", truncated);
    localItemObject.put("bmiddle_pic", bmiddle_pic);
    localItemObject.put("thumbnail_pic", thumbnail_pic);
    localItemObject.put("source", source);
    localItemObject.put("favorited", favorited);
    localItemObject.put("original_pic", original_pic);
    localItemObject.put("in_reply_to_status_id", in_reply_to_status_id);
    localItemObject.put("reposts_count", reposts_count);
    localItemObject.put("created_at", created_at);
    localItemObject.put("in_reply_to_user_id", in_reply_to_user_id);

    for (int i = 0; i < annotations.size(); i++) {
        String itemData = annotations.get(i);
        itemJSONArray.put(itemData);
    }//from  w  w  w .jav a2 s. c  om
    localItemObject.put("annotations", itemJSONArray);
    localItemObject.put("mid", mid);
    if (null != user) {
        localItemObject.put("user", user.toJson());
    }
    return localItemObject;
}

From source file:edu.stanford.junction.api.activity.ActivityScript.java

public void addRoleHint(String role, String hint) {
    try {/*from   w  w  w .  j a va 2  s  . c  om*/
        if (roleSpecs == null)
            roleSpecs = new JSONObject();
        if (!roleSpecs.has(role)) {
            roleSpecs.put(role, new JSONObject());
        }

        JSONObject jsonRole = roleSpecs.getJSONObject(role);
        if (!jsonRole.has("hints")) {
            jsonRole.put("hints", new JSONArray());
        }

        JSONArray jsonHints = jsonRole.getJSONArray("hints");
        jsonHints.put(hint);

        mJSON = null; // reset
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.graphhopper.http.NearestServlet.java

@Override
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes)
        throws ServletException, IOException {
    String pointStr = getParam(httpReq, "point", null);
    boolean enabledElevation = getBooleanParam(httpReq, "elevation", false);

    JSONObject result = new JSONObject();
    if (pointStr != null && !pointStr.equalsIgnoreCase("")) {
        GHPoint place = GHPoint.parse(pointStr);
        LocationIndex index = hopper.getLocationIndex();
        QueryResult qr = index.findClosest(place.lat, place.lon, EdgeFilter.ALL_EDGES);

        if (!qr.isValid()) {
            result.put("error", "Nearest point cannot be found!");
        } else {/*from  w  ww .jav  a  2 s . c  o  m*/
            GHPoint3D snappedPoint = qr.getSnappedPoint();
            result.put("type", "Point");

            JSONArray coord = new JSONArray();
            coord.put(snappedPoint.lon);
            coord.put(snappedPoint.lat);

            if (hopper.hasElevation() && enabledElevation)
                coord.put(snappedPoint.ele);

            result.put("coordinates", coord);

            // Distance from input to snapped point in meters
            result.put("distance", calc.calcDist(place.lat, place.lon, snappedPoint.lat, snappedPoint.lon));
        }
    } else {
        result.put("error", "No lat/lon specified!");
    }

    writeJson(httpReq, httpRes, result);
}

From source file:fi.harism.lucidchat.api.ChatConnection.java

/**
 * Send create_session message./*from   w  ww .  java2s. c o  m*/
 */
private void sendCreateSession() {
    try {
        mObserver.onServerMessage(new Message(Message.TYPE_LOG, "Send create session"));
        JSONObject createSession = new JSONObject();
        createSession.put("action", "create_session");

        // Setting user_id and user_auth will continue existing session.
        // This happens only when client disconnects unexpectedly. By
        // default new session is created when application starts.
        if (mUserId != null && mUserAuth != null) {
            createSession.put("user_id", mUserId);
            createSession.put("user_auth", mUserAuth);
        } else {
            JSONObject userAttrs = new JSONObject();
            userAttrs.put("name", mName);
            userAttrs.put("realname", mRealName);
            createSession.put("user_attrs", userAttrs);
        }

        JSONArray message_types = new JSONArray();
        message_types.put("*");
        createSession.put("message_types", message_types);

        mWSC.send(createSession.toString());
    } catch (JSONException ex) {
        ex.printStackTrace();
    }
}

From source file:org.chromium.ChromeSocket.java

private void getNetworkList(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
    try {//from   www  . ja v a2s  .c  om
        JSONArray list = new JSONArray();
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        NetworkInterface iface;
        // Enumerations are a crappy legacy API, can't use the for (foo : bar) syntax.
        while (interfaces.hasMoreElements()) {
            iface = interfaces.nextElement();
            if (iface.isLoopback()) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) {
                InetAddress address = interfaceAddress.getAddress();
                if (address != null) {
                    JSONObject data = new JSONObject();
                    data.put("name", iface.getDisplayName());
                    // Strip percent suffix off of ipv6 addresses to match desktop behaviour.
                    data.put("address", address.getHostAddress().replaceAll("%.*", ""));
                    data.put("prefixLength", interfaceAddress.getNetworkPrefixLength());
                    list.put(data);
                }
            }
        }

        callbackContext.success(list);
    } catch (SocketException se) {
        callbackContext.error("SocketException: " + se);
    }
}

From source file:io.github.grahambell.taco.TacoTransport.java

/**
 * Convert a Java Collection to a JSON array.
 *
 * Entries of the <code>Collection</code> are converted using the
 * {@link #objectToJson} method./* w  w w .  j a v a  2 s  .c  o  m*/
 *
 * @param list the input Collection
 * @return a JSON representation of the Collection
 * @throws TacoException on error in conversion
 */
public JSONArray collectionToJson(Collection<Object> list) throws TacoException {
    JSONArray json = new JSONArray();

    for (Object value : list) {
        json.put(objectToJson(value));
    }

    return json;
}