Example usage for org.json JSONArray toString

List of usage examples for org.json JSONArray toString

Introduction

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

Prototype

public String toString(int indentFactor) throws JSONException 

Source Link

Document

Make a prettyprinted JSON text of this JSONArray.

Usage

From source file:com.gmx.library.L.java

private static void printJson(String tag, String json, String headString) {
    if (TextUtils.isEmpty(json)) {
        d("Empty/Null json content");
        return;/*from  ww  w  .jav a 2 s  .c  o m*/
    }
    if (TextUtils.isEmpty(tag)) {
        tag = TAG;
    }
    String message;

    try {
        if (json.startsWith("{")) {
            JSONObject jsonObject = new JSONObject(json);
            message = jsonObject.toString(JSON_INDENT);
        } else if (json.startsWith("[")) {
            JSONArray jsonArray = new JSONArray(json);
            message = jsonArray.toString(JSON_INDENT);
        } else {
            message = json;
        }
    } catch (JSONException e) {
        message = json;
    }

    printLine(tag, true);
    message = headString + LINE_SEPARATOR + message;
    String[] lines = message.split(LINE_SEPARATOR);
    for (String line : lines) {
        Log.d(tag, "|" + line);
    }
    printLine(tag, false);
}

From source file:de.robbers.dashclock.stackextension.StackExtension.java

private void parseUserResponse(String json) {
    if (json == null) {
        return;/*from   ww w .  j  a va2s .co  m*/
    }
    try {
        JSONArray items = new JSONObject(json).getJSONArray("items");
        Log.i(TAG, items.toString(2));
        if (items.length() == 0) {
            mError = true;
            publishErrorUpdate(ERROR_USER_SITE_COMBINATION);
            return;
        }
        JSONObject user = items.getJSONObject(0);
        switch (mDisplay) {
        case DISPLAY_TOTAL_REP:
            mReputation = user.getInt("reputation");
            break;
        case DISPLAY_TODAYS_REP:
            mReputation = user.getInt("reputation_change_day");
            if (mReputation == 0) {
                mVisible = false;
            }
            break;
        }
    } catch (JSONException e) {
        Log.i(TAG, json);
        e.printStackTrace();
    }
}

From source file:de.robbers.dashclock.stackextension.StackExtension.java

private void parseReputationResponse(String json) {
    if (json == null) {
        return;// w  w w . ja  v a2s  . c o m
    }
    mExpandedBody = "";
    LongSparseArray reputationArray = new LongSparseArray();
    try {
        JSONArray items = new JSONObject(json).getJSONArray("items");
        Log.i(TAG, items.toString(2));

        for (int i = 0; i < items.length(); i++) {
            JSONObject reputation = items.getJSONObject(i);
            long postId = reputation.optLong("post_id");
            int reputationChange = reputation.optInt("reputation_change");
            int newValue = reputationChange;
            newValue += (Integer) reputationArray.get(postId, 0);
            reputationArray.put(postId, newValue);
        }

        List<Long> postIds = new ArrayList<Long>();
        for (int i = 0; i < items.length(); i++) {
            JSONObject reputation = items.getJSONObject(i);
            long postId = reputation.optLong("post_id");
            int reputationChange = reputation.optInt("reputation_change");
            if (postIds.contains(postId) || reputationChange == 0) {
                continue;
            }
            postIds.add(postId);
            int reputationValue = (Integer) reputationArray.get(postId);
            String title = String.valueOf(Html.fromHtml(reputation.optString("title")));
            mExpandedBody += buildExpandedBodyPost(reputationValue, title, postIds.size());
        }
    } catch (JSONException e) {
        Log.i(TAG, json);
        e.printStackTrace();
    }
    if (TextUtils.isEmpty(mExpandedBody)) {
        mExpandedBody = getString(R.string.no_recent_reputation_changes);
    }
}

From source file:com.novartis.opensource.yada.test.ServiceTest.java

/**
 * Convenience method to output results from tests
 * // ww  w. ja va2  s  . c  o  m
 * @param res the JSON array containing the query result
 */
public static void logJSONResult(JSONArray res) {
    logStringResult(res.toString(2));
}