Search in sources :

Example 6 with JSONTokener

use of org.json.JSONTokener in project phonegap-facebook-plugin by Wizcorp.

the class Utility method getStringPropertyAsJSON.

// Returns either a JSONObject or JSONArray representation of the 'key' property of 'jsonObject'.
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException {
    Object value = jsonObject.opt(key);
    if (value != null && value instanceof String) {
        JSONTokener tokener = new JSONTokener((String) value);
        value = tokener.nextValue();
    }
    if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) {
        if (nonJSONPropertyKey != null) {
            // Facebook sometimes gives us back a non-JSON value such as
            // literal "true" or "false" as a result.
            // If we got something like that, we present it to the caller as
            // a GraphObject with a single
            // property. We only do this if the caller wants that behavior.
            jsonObject = new JSONObject();
            jsonObject.putOpt(nonJSONPropertyKey, value);
            return jsonObject;
        } else {
            throw new FacebookException("Got an unexpected non-JSON object.");
        }
    }
    return value;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject)

Example 7 with JSONTokener

use of org.json.JSONTokener in project SmartAndroidSource by jaychou2012.

the class JsonHttpResponseHandler method parseResponse.

/**
     * Returns Object of type {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long,
     * Double or {@link JSONObject#NULL}, see {@link org.json.JSONTokener#nextValue()}
     *
     * @param responseBody response bytes to be assembled in String and parsed as JSON
     * @return Object parsedResponse
     * @throws org.json.JSONException exception if thrown while parsing JSON
     */
protected Object parseResponse(byte[] responseBody) throws JSONException {
    if (null == responseBody)
        return null;
    Object result = null;
    //trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If JSON is not valid this will return null
    String jsonString = getResponseString(responseBody, getCharset());
    if (jsonString != null) {
        jsonString = jsonString.trim();
        if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
            result = new JSONTokener(jsonString).nextValue();
        }
    }
    if (result == null) {
        result = jsonString;
    }
    return result;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject)

Example 8 with JSONTokener

use of org.json.JSONTokener in project restful-android by jeremyhaberman.

the class TwitterTimelineTest method setUp.

@Override
protected void setUp() throws Exception {
    if (timelineData == null) {
        InputStream is = getInstrumentation().getContext().getResources().openRawResource(R.raw.timeline_response);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        String json = writer.toString();
        JSONTokener tokener = new JSONTokener(json);
        timelineData = (JSONArray) tokener.nextValue();
    }
}
Also used : JSONTokener(org.json.JSONTokener) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 9 with JSONTokener

use of org.json.JSONTokener in project Klyph by jonathangerbaud.

the class Response method createResponsesFromString.

static List<Response> createResponsesFromString(String responseString, HttpURLConnection connection, RequestBatch requests, boolean isFromCache) throws FacebookException, JSONException, IOException {
    JSONTokener tokener = new JSONTokener(responseString);
    Object resultObject = tokener.nextValue();
    List<Response> responses = createResponsesFromObject(connection, requests, resultObject, isFromCache);
    Logger.log(LoggingBehavior.REQUESTS, RESPONSE_LOG_TAG, "Response\n  Id: %s\n  Size: %d\n  Responses:\n%s\n", requests.getId(), responseString.length(), responses);
    return responses;
}
Also used : JSONTokener(org.json.JSONTokener) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject)

Example 10 with JSONTokener

use of org.json.JSONTokener in project zxingfragmentlib by mitoyarzun.

the class BookResultInfoRetriever method retrieveSupplementalInfo.

@Override
void retrieveSupplementalInfo() throws IOException {
    CharSequence contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON);
    if (contents.length() == 0) {
        return;
    }
    String title;
    String pages;
    Collection<String> authors = null;
    try {
        JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
        JSONArray items = topLevel.optJSONArray("items");
        if (items == null || items.isNull(0)) {
            return;
        }
        JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
        if (volumeInfo == null) {
            return;
        }
        title = volumeInfo.optString("title");
        pages = volumeInfo.optString("pageCount");
        JSONArray authorsArray = volumeInfo.optJSONArray("authors");
        if (authorsArray != null && !authorsArray.isNull(0)) {
            authors = new ArrayList<>(authorsArray.length());
            for (int i = 0; i < authorsArray.length(); i++) {
                authors.add(authorsArray.getString(i));
            }
        }
    } catch (JSONException e) {
        throw new IOException(e);
    }
    Collection<String> newTexts = new ArrayList<>();
    maybeAddText(title, newTexts);
    maybeAddTextSeries(authors, newTexts);
    maybeAddText(pages == null || pages.isEmpty() ? null : pages + "pp.", newTexts);
    String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) + "/search?tbm=bks&source=zxing&q=";
    append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

JSONTokener (org.json.JSONTokener)62 JSONObject (org.json.JSONObject)59 JSONException (org.json.JSONException)31 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)9 GraphObject (com.facebook.model.GraphObject)8 FileInputStream (java.io.FileInputStream)7 File (java.io.File)6 HashMap (java.util.HashMap)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)4 HttpPost (org.apache.http.client.methods.HttpPost)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 FacebookException (com.facebook.FacebookException)3