Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

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

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:com.ichi2.libanki.importer.Anki2Importer.java

private long _mid(long srcMid) {
    try {//  w ww  . j  av a 2 s  . c  om
        // already processed this mid?
        if (mModelMap.containsKey(srcMid)) {
            return mModelMap.get(srcMid);
        }
        long mid = srcMid;
        JSONObject srcModel = new JSONObject(Utils.jsonToString(mSrc.getModels().get(srcMid)));
        String srcScm = mSrc.getModels().scmhash(srcModel);
        while (true) {
            // missing from target col?
            if (!mDst.getModels().have(mid)) {
                // copy it over
                JSONObject model = new JSONObject(Utils.jsonToString(srcModel));
                model.put("id", mid);
                model.put("mod", Utils.intNow());
                model.put("usn", mCol.usn());
                mDst.getModels().update(model);
                break;
            }
            // there's an existing model; do the schemas match?
            JSONObject dstModel = new JSONObject(Utils.jsonToString(mDst.getModels().get(mid)));
            String dstScm = mDst.getModels().scmhash(dstModel);
            if (srcScm.equals(dstScm)) {
                // they do; we can reuse this mid
                break;
            }
            // as they don't match, try next id
            mid += 1;
        }
        mModelMap.put(srcMid, mid);
        return mid;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ichi2.libanki.importer.Anki2Importer.java

/** Decks */

/* Given did in src col, return local id */
private long _did(long did) {
    try {//from  ww  w  .  ja  v  a 2s .co  m
        // already converted?
        if (mDecks.containsKey(did)) {
            return mDecks.get(did);
        }
        // get the name in src
        JSONObject g = mSrc.getDecks().get(did);
        String name = g.getString("name");
        // if there's a prefix, replace the top level deck
        if (mDeckPrefix != null) {
            String[] tmpname = name.split("::", -1);
            name = mDeckPrefix;
            if (tmpname.length > 1) {
                for (int i = 0; i < tmpname.length - 2; i++) {
                    name += "::" + tmpname[i + 1];
                }
            }
        }
        // Manually create any parents so we can pull in descriptions
        String head = "";
        String[] parents = name.split("::", -1);
        for (int i = 0; i < parents.length - 1; ++i) {
            if (head.length() > 0) {
                head = head.concat("::");
            }
            head = head.concat(parents[i]);
            long idInSrc = mSrc.getDecks().id(head);
            _did(idInSrc);
        }
        // create in local
        long newid = mDst.getDecks().id(name);
        // pull conf over
        if (g.has("conf") && g.getLong("conf") != 1) {
            JSONObject conf = mSrc.getDecks().getConf(g.getLong("conf"));
            mDst.getDecks().save(conf);
            mDst.getDecks().updateConf(conf);
            JSONObject g2 = mDst.getDecks().get(newid);
            g2.put("conf", g.getLong("conf"));
            mDst.getDecks().save(g2);
        }
        // save desc
        JSONObject deck = mDst.getDecks().get(newid);
        deck.put("desc", g.getString("desc"));
        mDst.getDecks().save(deck);
        // add to deck map and return
        mDecks.put(did, newid);
        return newid;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.asd.littleprincesbeauty.data.TaskList.java

@Override
public JSONObject getCreateAction(int actionId) {
    JSONObject js = new JSONObject();

    try {//w  ww.j  a  v  a  2s. c  om
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);

        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

        // index
        js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);

        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
        entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate tasklist-create jsonobject");
    }

    return js;
}

From source file:com.asd.littleprincesbeauty.data.TaskList.java

@Override
public JSONObject getUpdateAction(int actionId) {
    JSONObject js = new JSONObject();

    try {/*from www .j  a v  a  2 s . co m*/
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);

        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

        // id
        js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());

        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate tasklist-update jsonobject");
    }

    return js;
}

From source file:com.asd.littleprincesbeauty.data.TaskList.java

@Override
public JSONObject getLocalJSONFromContent() {
    try {// ww  w.  j  a v a  2  s.  c  om
        JSONObject js = new JSONObject();
        JSONObject folder = new JSONObject();

        String folderName = getName();
        if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX))
            folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(),
                    folderName.length());
        folder.put(NoteColumns.SNIPPET, folderName);
        if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT)
                || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE))
            folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
        else
            folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);

        js.put(GTaskStringUtils.META_HEAD_NOTE, folder);

        return js;
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        return null;
    }
}

From source file:com.citruspay.mobile.payment.oauth2.OAuth2Token.java

public static OAuth2Token create(JSONObject json) {
    // enhance object w/ expiry date
    JSONObject jtoken = json;
    if (!json.has("expiry")) {
        // copy input
        jtoken = new JSONObject();
        for (Iterator<?> keys = json.keys(); keys.hasNext();) {
            String key = (String) keys.next();
            try {
                jtoken.put(key, json.get(key));
            } catch (JSONException jx) {
                throw new RuntimeException(jx);
            }//from  www .jav  a  2 s  .com
        }

        // add expiry date
        long expiry = new Date().getTime() / 1000l;
        try {
            expiry += json.getLong("expires_in");
        } catch (JSONException jx) {
            /* ignore => expires now ! */
        }
        try {
            jtoken.put("expiry", expiry);
        } catch (JSONException jx) {
            throw new RuntimeException(jx);
        }
    }

    // create token
    return new OAuth2Token(jtoken);
}

From source file:com.neka.cordova.inappbrowser.InAppBrowser.java

/**
 * Closes the dialog/*from   ww  w  .ja v  a  2  s .  c om*/
 */
public void closeDialog() {
    final WebView childView = this.inAppWebView;
    // The JS protects against multiple calls, so this should happen only when
    // closeDialog() is called by other native code.
    if (childView == null) {
        return;
    }
    this.cordova.getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            childView.setWebViewClient(new WebViewClient() {
                // NB: wait for about:blank before dismissing
                public void onPageFinished(WebView view, String url) {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }
            });
            // NB: From SDK 19: "If you call methods on WebView from any thread 
            // other than your app's UI thread, it can cause unexpected results."
            // http://developer.android.com/guide/webapps/migrating.html#Threads
            childView.loadUrl("about:blank");
        }
    });

    try {
        JSONObject obj = new JSONObject();
        obj.put("type", EXIT_EVENT);
        sendUpdate(obj, false);
    } catch (JSONException ex) {
        Log.d(LOG_TAG, "Should never happen");
    }
}

From source file:org.charvolant.argushiigi.server.TypeQueryResource.java

/**
 * Get a list of matching resources as a JSON document.
 *
 * @see org.restlet.resource.ServerResource#get()
 */// w w  w  .  j a v a 2  s .  c o m
@Get
public Representation get() {
    String type = Reference.decode(this.getQueryValue(this.PARAM_TYPE));

    try {
        JSONObject wrap = new JSONObject();
        JSONArray result = new JSONArray();
        StmtIterator si = this.model.listStatements(null, RDF.type, this.model.createResource(type));
        Comparator<Resource> classComparator = this.sorter.getClassComparator();

        while (si.hasNext()) {
            Statement s = si.next();
            Resource res = s.getSubject();

            if (res.isURIResource()) {
                JSONObject obj = new JSONObject();
                Reference ref = this.application.getLocalRef(new Reference(res.getURI()));
                StmtIterator ci = this.model.listStatements(res, RDF.type, (Resource) null);
                Resource cls = null;

                obj.put("name", this.sorter.getName(s.getSubject(), Locale.ENGLISH));
                obj.put("uri", s.getSubject().getURI());
                obj.put("href", ref);
                while (ci.hasNext()) {
                    Resource c = ci.next().getResource();

                    if (c.isAnon())
                        continue;
                    if (cls == null || classComparator.compare(cls, c) > 0)
                        cls = c;
                }
                if (cls == null)
                    cls = OWL.Thing;
                ref = this.application.getLocalRef(new Reference(cls.getURI()));
                obj.put("cls", this.sorter.getName(cls, Locale.ENGLISH));
                obj.put("clsUri", cls.getURI());
                obj.put("clsHref", ref);
                result.put(obj);
            }
        }
        wrap.put("aaData", result);
        return new JsonRepresentation(wrap);
    } catch (Exception ex) {
        this.application.getLogger().log(Level.SEVERE, "Unable to get resources of type " + type, ex);
        this.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, ex);
        return new StringRepresentation(ex.getMessage());
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java

static WebRequest getPutGitConfigRequest(String location, String value)
        throws JSONException, UnsupportedEncodingException {
    String requestURI = toAbsoluteURI(location);
    JSONObject body = new JSONObject();
    body.put(GitConstants.KEY_CONFIG_ENTRY_VALUE, value);
    WebRequest request = new PutMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()),
            "UTF-8");
    request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
    setAuthentication(request);// w  ww  . j av  a2s.  c  o  m
    return request;
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java

static WebRequest getPostGitConfigRequest(String location, String key, String value)
        throws JSONException, UnsupportedEncodingException {
    String requestURI = toAbsoluteURI(location);
    JSONObject body = new JSONObject();
    body.put(GitConstants.KEY_CONFIG_ENTRY_KEY, key);
    body.put(GitConstants.KEY_CONFIG_ENTRY_VALUE, value);
    WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()),
            "UTF-8");
    request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
    setAuthentication(request);//from  w ww  .  j  ava 2 s. co  m
    return request;
}