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.android.i18n.addressinput.RegionDataConstants.java

/**
 * Assumes the array is a well-formed array - i.e., there are no unmatched keys in the input.
 * Package-private so it can be accessed by tests.
 *//*from  w w w  .j  av a2 s. co  m*/
static String convertArrayToJsonString(String[] input) {
    JSONObject object = new JSONObject();
    for (int i = 0; i < input.length; i += 2) {
        try {
            object.put(input[i], input[i + 1]);
        } catch (JSONException e) {
            // Ignore for now.
        }
    }
    return object.toString();
}

From source file:fm.moe.android.util.JSONFileHelper.java

public static void write(final JSONObject object, final String path) throws IOException {
    if (object == null || path == null)
        return;/* ww  w.ja  v a  2s .c  o m*/
    new File(path).delete();
    final RandomAccessFile raf = new RandomAccessFile(path, FILE_MODE_RW);
    final FileWriter fw = new FileWriter(raf.getFD());
    fw.write(object.toString());
    fw.flush();
    fw.close();
}

From source file:sandeep.kb.android.remote.android.AndroidWebDriver.java

private Map<Object, Object> convertJsonObjectToMap(final JSONObject json) {
    Map<Object, Object> toReturn = Maps.newHashMap();
    for (Iterator it = json.keys(); it.hasNext();) {
        String key = (String) it.next();
        try {//from  w  w  w  .  ja  v a 2 s  . c  om
            Object value = json.get(key);
            toReturn.put(convertJsonToJavaObject(key), convertJsonToJavaObject(value));
        } catch (JSONException e) {
            throw new RuntimeException("Failed to parse JSON:" + json.toString(), e);
        }
    }
    return toReturn;
}

From source file:sandeep.kb.android.remote.android.AndroidWebDriver.java

private void throwIfError(final JSONObject jsonObject) {
    int status;//from w  w  w .j  av  a  2  s .co m
    String errorMsg;
    try {
        status = (Integer) jsonObject.get(STATUS);
        errorMsg = String.valueOf(jsonObject.get(VALUE));
    } catch (JSONException e) {
        throw new RuntimeException("Failed to parse JSON Object: " + jsonObject, e);
    }
    switch (status) {
    case ErrorCodes.SUCCESS:
        return;
    case ErrorCodes.NO_SUCH_ELEMENT:
        throw new NoSuchElementException("Could not find " + "WebElement.");
    case ErrorCodes.STALE_ELEMENT_REFERENCE:
        throw new StaleElementReferenceException("WebElement is stale.");
    default:
        if (jsonObject.toString()
                .contains("Result of expression 'd.evaluate' [undefined] is" + " not a function.")) {
            throw new WebDriverException("You are using a version of Android WebDriver APK"
                    + " compatible with ICS SDKs or more recent SDKs. For more info take a look at"
                    + " http://code.google.com/p/selenium/wiki/AndroidDriver#Supported_Platforms. Error:" + " "
                    + jsonObject.toString());
        }
        throw new WebDriverException("Error: " + errorMsg);
    }
}