Example usage for org.json JSONObject toString

List of usage examples for org.json JSONObject toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Make a JSON text of this JSONObject.

Usage

From source file:com.mikecorrigan.trainscorekeeper.Game.java

public boolean write(final Context context) {
    Log.vc(VERBOSE, TAG, "write: context=" + context);

    // Ignore any undo events.
    List<ScoreEvent> temp = scoreEvents = scoreEvents.subList(0, lastScoreEvent);

    // Convert the list of events to a JSON array, then convert to a string.
    String string = null;/*from w ww  .  j  a v  a  2s  .  c  o  m*/
    try {
        JSONArray jsonScoreEvents = new JSONArray();
        for (ScoreEvent scoreEvent : temp) {
            JSONObject jsonScoreEvent = scoreEvent.toJson();
            if (jsonScoreEvent == null) {
                Log.w(TAG, "write: skipping score event");
                continue;
            }

            jsonScoreEvents.put(jsonScoreEvents.length(), jsonScoreEvent);
        }

        JSONObject jsonRoot = new JSONObject();
        jsonRoot.put(JsonSpec.SCORES_KEY, jsonScoreEvents);
        jsonRoot.put(JsonSpec.GAME_SPEC, spec);

        string = jsonRoot.toString();
    } catch (JSONException e) {
        Log.th(TAG, e, "write: failed");
        return false;
    }

    // Write the string into a file.
    if (string != null) {
        String path = context.getFilesDir() + File.separator + SAVED_FILE;
        File file = new File(path);
        FileUtils.writeStringToFile(file, string);
    }

    return true;
}

From source file:com.skysql.manager.api.SystemInfo.java

/**
 * Sets a property.//from  www.  j a v  a 2 s.c  o m
 *
 * @param property the property
 * @param value the value
 * @return true, if successful
 */
public boolean setProperty(String property, String value) {
    APIrestful api = new APIrestful();

    try {
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("value", value);
        if (api.put("system/" + currentID + "/property/" + property, jsonParam.toString())) {
            WriteResponse writeResponse = APIrestful.getGson().fromJson(api.getResult(), WriteResponse.class);
            if (writeResponse != null
                    && (!writeResponse.getInsertKey().isEmpty() || writeResponse.getUpdateCount() > 0)) {
                return true;
            }
        }
    } catch (JSONException e) {
        new ErrorDialog(e, "Error encoding API request");
        throw new RuntimeException("Error encoding API request");
    }

    return false;
}

From source file:org.jboss.aerogear.cordova.push.PushPlugin.java

/**
 * Serializes a bundle to JSON./*from ww  w  . j  a v a2 s .c  o  m*/
 * @param message to be serialized
 */
private static JSONObject convertBundleToJson(Bundle message) {
    try {
        JSONObject json;
        json = new JSONObject();

        JSONObject jsondata = new JSONObject();
        for (String key : message.keySet()) {
            Object value = message.get(key);

            // System data from Android
            if (key.equals("from") || key.equals("collapse_key")) {
                json.put(key, value);
            } else if (key.equals("foreground")) {
                json.put(key, message.getBoolean("foreground"));
            } else if (key.equals("coldstart")) {
                json.put(key, message.getBoolean("coldstart"));
            } else {
                // Maintain backwards compatibility
                if (key.equals("message") || key.equals("msgcnt") || key.equals("sound")
                        || key.equals("alert")) {
                    json.put(key, value);
                }

                if (value instanceof String) {
                    // Try to figure out if the value is another JSON object

                    String strValue = (String) value;
                    if (strValue.startsWith("{")) {
                        try {
                            JSONObject json2 = new JSONObject(strValue);
                            jsondata.put(key, json2);
                        } catch (Exception e) {
                            jsondata.put(key, value);
                        }
                        // Try to figure out if the value is another JSON array
                    } else if (strValue.startsWith("[")) {
                        try {
                            JSONArray json2 = new JSONArray(strValue);
                            jsondata.put(key, json2);
                        } catch (Exception e) {
                            jsondata.put(key, value);
                        }
                    } else {
                        jsondata.put(key, value);
                    }
                }
            }
        } // while
        json.put("payload", jsondata);

        Log.v(TAG, "extrasToJSON: " + json.toString());

        return json;
    } catch (JSONException e) {
        Log.e(TAG, "extrasToJSON: JSON exception");
    }
    return null;
}

From source file:com.tuarua.AVANEContext.java

public AVANEContext() {
    libAVANE.eventDispatcher.addEventListener(Event.TRACE, new IEventHandler() {
        @Override// w  w  w .j  a  va  2 s .  com
        public void callback(Event event) {
            trace((String) event.getParams());
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.INFO, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync(String.valueOf(event.getParams()), Event.INFO);
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.INFO_HTML, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync(String.valueOf(event.getParams()), Event.INFO_HTML);
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.ON_PROBE_INFO_AVAILABLE, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync("", Event.ON_PROBE_INFO);
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.NO_PROBE_INFO, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync("", Event.NO_PROBE_INFO);
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.ON_ENCODE_START, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync("", Event.ON_ENCODE_START);
        }
    });

    libAVANE.eventDispatcher.addEventListener(Event.ON_ENCODE_ERROR, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync(String.valueOf(event.getParams()), Event.ON_ENCODE_ERROR);
        }
    });

    libAVANE.eventDispatcher.addEventListener(Event.ON_ERROR_MESSAGE, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync(String.valueOf(event.getParams()), Event.ON_ERROR_MESSAGE);
        }
    });

    libAVANE.eventDispatcher.addEventListener(Event.ON_ENCODE_FATAL, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync(String.valueOf(event.getParams()), Event.ON_ENCODE_FATAL);
        }
    });

    libAVANE.eventDispatcher.addEventListener(Event.ON_ENCODE_FINISH, new IEventHandler() {
        @Override
        public void callback(Event event) {
            dispatchStatusEventAsync("", Event.ON_ENCODE_FINISH);
        }
    });
    libAVANE.eventDispatcher.addEventListener(Event.ON_ENCODE_PROGRESS, new IEventHandler() {
        @Override
        public void callback(Event event) {
            final Progress progress = (Progress) event.getParams();
            JSONObject obj = new JSONObject();
            try {
                obj.put("bitrate", progress.bitrate);
                obj.put("frame", progress.frame);
                obj.put("fps", progress.fps);
                obj.put("secs", progress.secs);
                obj.put("size", progress.size);
                obj.put("speed", progress.speed);
                obj.put("us", progress.us);
                dispatchStatusEventAsync(obj.toString(), Event.ON_ENCODE_PROGRESS);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

}

From source file:networkedassets.PhilipsHue.java

public void createGroup() throws IOException {

    JSONObject groups = Useful.readJsonFromUrl(hueIp + "api/newdeveloper/groups/");
    Iterator<String> keys = groups.keys();

    while (keys.hasNext()) {
        String id = keys.next();/* w ww  .jav a 2 s . com*/
        System.out.println(Useful.http("", hueIp + "api/newdeveloper/groups/" + id, "DELETE"));
        System.out.println(id);

    }

    JSONObject obj = new JSONObject();
    obj.put("name", "blah");
    List indexes = new ArrayList();
    for (Object pl : allLights) {
        int id = ((PhilipsLight) pl).id;
        indexes.add(String.valueOf(id));
    }
    obj.put("lights", indexes);
    System.out.println(obj.toString());

    System.out.println(Useful.http(obj.toString(), hueIp + "api/newdeveloper/groups/", "POST"));

}

From source file:at.tugraz.iaik.magnum.conf.MethodHookConfig.java

public String toJSonString() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(NAME_METHOD, methodName);//ww  w  .  ja va 2  s.  co  m
    json.put(TYPE, type);
    json.put(NUM_INVOCATIONS, numInvocations);
    return json.toString();
}

From source file:com.marketcloud.marketcloud.Addresses.java

/**
 * Creates a basic new address.//from   w w  w  .ja  v  a 2s. c  o  m
 *
 * @param name name
 * @param email e-mail address
 * @return the new address
 */
@SuppressWarnings("unused")
public JSONObject create(String name, String email)
        throws NullPointerException, ExecutionException, InterruptedException, JSONException {
    if (tm.getSessionToken() != null) {
        JSONObject jo = toJsonObject(name, email);

        if (jo != null)
            return new Connect(context).run("post", "http://api.marketcloud.it/v0/addresses",
                    publicKey + ":" + tm.getSessionToken(), jo.toString());

    }
    return null;
}

From source file:com.marketcloud.marketcloud.Addresses.java

/**
 * Create a new address.//  ww w.j a v a 2s .  c  o  m
 *<br />
 * You have to provide the required parameters "full_name" and "email".<br />
 * You can add the following optional information:<br />
 * - country,<br />
 * - state,<br />
 * - city,<br />
 * - address1,<br />
 * - address2,<br />
 * - postal_code,<br />
 * - phone_number,<br />
 * - alternate_phone_number.<br />
 *<br />
 * Example:<br />
 * {<br />
 *     "full_name" : "John Smith",<br />
 *     "email" : "johnsmith@example.com",<br />
 *     "country" : "USA"<br />
 *     "state" : "California",<br />
 *     "city" : "Los Angeles",<br />
 *     "address1" : "Example Street, 1",<br />
 *     "postal_code" : "90096",<br />
 *     "phone_number" : "0 123 456 789"<br />
 * }<br />
 *
 * @param jsonObject the address data
 * @return the new address
 */
@SuppressWarnings("unused")
public JSONObject create(JSONObject jsonObject)
        throws NullPointerException, ExecutionException, InterruptedException, JSONException {
    if (tm.getSessionToken() != null)
        return new Connect(context).run("post", "http://api.marketcloud.it/v0/addresses",
                publicKey + ":" + tm.getSessionToken(), jsonObject.toString());

    return null;
}

From source file:com.marketcloud.marketcloud.Addresses.java

/**
 * Updates an address./*from  w  ww  .  j ava  2  s .c  o m*/
 *
 * @param id address id
 * @param name new name
 * @param email new email
 * @return the data of the updated address
 */
@SuppressWarnings("unused")
public JSONObject update(int id, String name, String email)
        throws NullPointerException, ExecutionException, InterruptedException, JSONException {
    if (tm.getSessionToken() != null) {
        JSONObject jo = toJsonObject(name, email);

        if (jo != null)
            return new Connect(context).run("put", "http://api.marketcloud.it/v0/addresses/" + id,
                    publicKey + ":" + tm.getSessionToken(), jo.toString());
    }

    return null;
}

From source file:com.marketcloud.marketcloud.Addresses.java

/**
 * Update an address./* w w  w  .  j av a2s .c o m*/
 *<br />
 * You can choose which information to update, between:<br />
 * - full_name,<br />
 * - email,<br />
 * - country,<br />
 * - state,<br />
 * - city,<br />
 * - address1,<br />
 * - address2,<br />
 * - postal_code,<br />
 * - phone_number,<br />
 * - alternate_phone_number.<br />
 *<br />
 * Example:<br />
 * {<br />
 *     "address1" : "New Road, 1",<br />
 *     "phone_number" : "0 987 654 321"<br />
 * }<br />
 *
 * @param id address id
 * @param jsonObject information to update
 * @return the updated address
 */
@SuppressWarnings("unused")
public JSONObject update(int id, JSONObject jsonObject)
        throws NullPointerException, ExecutionException, InterruptedException, JSONException {
    if (tm.getSessionToken() != null)
        return new Connect(context).run("put", "http://api.marketcloud.it/v0/addresses/" + id,
                publicKey + ":" + tm.getSessionToken(), jsonObject.toString());

    return null;
}