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.ecml.MidiOptions.java

/** Convert this MidiOptions object into a JSON string. */
public String toJson() {
    try {/*from  ww w  .  java2  s.  co m*/
        JSONObject json = new JSONObject();
        JSONArray jsonTracks = new JSONArray();
        for (boolean value : tracks) {
            jsonTracks.put(value);
        }
        JSONArray jsonMute = new JSONArray();
        for (boolean value : mute) {
            jsonMute.put(value);
        }
        JSONArray jsonInstruments = new JSONArray();
        for (int value : instruments) {
            jsonInstruments.put(value);
        }
        if (time != null) {
            JSONObject jsonTime = new JSONObject();
            jsonTime.put("numerator", time.getNumerator());
            jsonTime.put("denominator", time.getDenominator());
            jsonTime.put("quarter", time.getQuarter());
            jsonTime.put("tempo", time.getTempo());
            json.put("time", jsonTime);
        }

        json.put("versionCode", 7);
        json.put("tracks", jsonTracks);
        json.put("mute", jsonMute);
        json.put("instruments", jsonInstruments);
        json.put("useDefaultInstruments", useDefaultInstruments);
        json.put("scrollVert", scrollVert);
        json.put("showPiano", showPiano);
        json.put("showNoteColors", showNoteColors);
        json.put("showLyrics", showLyrics);
        json.put("delay", delay);
        json.put("twoStaffs", twoStaffs);
        json.put("showNoteLetters", showNoteLetters);
        json.put("transpose", transpose);
        json.put("key", key);
        json.put("combineInterval", combineInterval);
        json.put("shade1Color", shade1Color);
        json.put("shade2Color", shade2Color);
        json.put("showMeasures", showMeasures);
        json.put("playMeasuresInLoop", playMeasuresInLoop);
        json.put("playMeasuresInLoopStart", playMeasuresInLoopStart);
        json.put("playMeasuresInLoopEnd", playMeasuresInLoopEnd);

        return json.toString();
    } catch (JSONException e) {
        return null;
    } catch (NullPointerException e) {
        return null;
    }
}

From source file:blink.Message.java

/**
 * a Message object that represents a message to be sent
 * @param timestamp when a message was created
 * @param text the actual content of the message
 * @param userId the id number for the user sending the message
 *//*from  w ww .  j a  v  a 2 s .c  o  m*/
public Message(long timestamp, String text, int userId) {
    this.timestamp = timestamp;
    this.text = text;
    this.userId = userId;
    this.type = "message";
    JSONObject json = new JSONObject();
    try {
        json.put("timestamp", timestamp);
        json.put("content", text);
        json.put("type", "message");
    } catch (JSONException ex) {
        Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
    }
    this.json = json.toString();
}

From source file:com.andybotting.tramhunter.objects.FavouriteList.java

/**
 * Convert an old favourites string to the new JSON format
 * @param favouriteString//from   ww w . ja v a2  s  . c  om
 * @return JSONArray
 */
private String convertOldFavourites(String favouriteString) {

    try {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        int tramTrackerId;

        StringTokenizer tokenizer = new StringTokenizer(favouriteString, ",");
        while (tokenizer.hasMoreTokens()) {
            tramTrackerId = Integer.parseInt(tokenizer.nextToken());
            jsonObject = new JSONObject();
            jsonObject.put("stop", tramTrackerId);
            jsonArray.put(jsonObject);
        }
        return jsonArray.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.cmgapps.android.util.CMGAppRater.java

private static String ratePreferenceToString(SharedPreferences pref) {
    JSONObject thiz = new JSONObject();
    try {/*from ww w . jav a  2  s . c o  m*/
        thiz.put(DECLINED_RATE, pref.getBoolean(DECLINED_RATE, false));
        thiz.put(APP_RATED, pref.getBoolean(APP_RATED, false));
        thiz.put(TRACKING_VERSION, pref.getInt(TRACKING_VERSION, -1));
        thiz.put(FIRST_USE,
                SimpleDateFormat.getDateTimeInstance().format(new Date(pref.getLong(FIRST_USE, 0L))));
        thiz.put(USE_COUNT, pref.getInt(USE_COUNT, 0));
        thiz.put(REMIND_LATER_DATE,
                SimpleDateFormat.getDateTimeInstance().format(new Date(pref.getLong(REMIND_LATER_DATE, 0L))));
    } catch (JSONException exc) {
        LOGE(TAG, "Error creating JSON Object", exc);
    }

    return thiz.toString();
}

From source file:de.kp.ames.web.function.transform.cache.XslCacheManager.java

/**
 * This method retrieves the actually uploaded transformators 
 * that are currently not registered in the OASIS ebXML RegRep
 * /*w w  w . j a v a2 s. com*/
 * @return
 * @throws Exception
 */
public JSONArray getJEntries() throws Exception {

    JSONArray jEntries = new JSONArray();

    List<CacheEntry> transformators = cache.getAll();
    for (int ix = 0; ix < transformators.size(); ix++) {

        XslTransformator transformator = (XslTransformator) transformators.get(ix);
        String key = transformator.getKey();

        JSONObject jTransformator = new JSONObject();

        jTransformator.put(JsonConstants.J_KEY, key);
        jTransformator.put(JsonConstants.J_NAME, transformator.getName());

        jTransformator.put(JsonConstants.J_DESC, "No description available.");
        jTransformator.put(JsonConstants.J_MIME, transformator.getMimetype());

        jEntries.put(jEntries.length(), jTransformator);

    }

    return jEntries;

}

From source file:CNCEntities.ResponseRequestCheckCount.java

@Override
public JSONObject getJsonEntity() {
    JSONObject jo = new JSONObject();
    try {/* ww w  . j  av a  2s .  c  om*/
        jo.put("code", code);
        jo.put("msg", msg);
        jo.put("listprovider", listprovider);
    } catch (JSONException ex) {
        Logger.getLogger(ResponseRequestCheckCount.class.getName()).log(Level.SEVERE, null, ex);
    }
    return jo;
}

From source file:org.rapidandroid.activity.FormCreator.java

private void saveState() {
    // TODO Auto-generated method stub
    // this.d/* www.j a  va2  s  .  c o  m*/
    JSONObject savedstate = new JSONObject();
    EditText etxFormName = (EditText) findViewById(R.id.etx_formname);
    EditText etxFormPrefix = (EditText) findViewById(R.id.etx_formprefix);
    EditText etxDescription = (EditText) findViewById(R.id.etx_description);
    //      ListView lsv = (ListView) findViewById(R.id.lsv_createfields);

    try {
        savedstate.put(STATE_FORMNAME, etxFormName.getText().toString());
        savedstate.put(STATE_PREFIX, etxFormPrefix.getText().toString());
        savedstate.put(STATE_DESC, etxDescription.getText().toString());
        savedstate.put(STATE_PARSER, mChosenParser.getName());

        if (mCurrentFields != null) {
            int numFields = this.mCurrentFields.size();
            for (int i = 0; i < numFields; i++) {
                Field f = mCurrentFields.get(i);

                JSONObject fieldobj = new JSONObject();

                fieldobj.put(AddField.ResultConstants.RESULT_KEY_FIELDNAME, f.getName());
                fieldobj.put(AddField.ResultConstants.RESULT_KEY_DESCRIPTION, f.getDescription());
                fieldobj.put(AddField.ResultConstants.RESULT_KEY_FIELDTYPE_ID,
                        ((SimpleFieldType) f.getFieldType()).getId());
                savedstate.put("Field" + i, fieldobj);
            }
        }

        getIntent().putExtra("current_form", savedstate.toString());

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        Log.d("FormCreator", e1.getMessage());
    }
}

From source file:net.jmhertlein.mcanalytics.api.request.PasswordResetRequest.java

@Override
public String toJSON() {
    JSONObject ret = new JSONObject();
    ret.put("id", getRequestId());
    ret.put("type", RequestType.PASSWORD_RESET);
    ret.put("username", username);
    ret.put("old", oldPass);
    ret.put("new", newPass);
    return ret.toString();
}

From source file:com.liferay.mobile.android.v62.assetcategory.AssetCategoryService.java

public JSONObject addCategory(String title, long vocabularyId, JSONObjectWrapper serviceContext)
        throws Exception {
    JSONObject _command = new JSONObject();

    try {//from  www  . j a v a2s. c  om
        JSONObject _params = new JSONObject();

        _params.put("title", checkNull(title));
        _params.put("vocabularyId", vocabularyId);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.service.ServiceContext", serviceContext);

        _command.put("/assetcategory/add-category", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v62.assetcategory.AssetCategoryService.java

public JSONObject addCategory(long parentCategoryId, JSONObject titleMap, JSONObject descriptionMap,
        long vocabularyId, JSONArray categoryProperties, JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*  ww  w.j a  v  a  2s .  c o m*/
        JSONObject _params = new JSONObject();

        _params.put("parentCategoryId", parentCategoryId);
        _params.put("titleMap", checkNull(titleMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        _params.put("vocabularyId", vocabularyId);
        _params.put("categoryProperties", checkNull(categoryProperties));
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.service.ServiceContext", serviceContext);

        _command.put("/assetcategory/add-category", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}