Sample code for 30+ languages & platforms
Android™

Pretty Print JSON (Formatter, Beautifier)

See more JSON Examples

Demonstrates how to emit JSON in a pretty, human-readable format with indenting of nested arrays and objects.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkJsonObject json = new CkJsonObject();

    String jsonStr = "{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}";

    success = json.Load(jsonStr);
    if (success != true) {
        Log.i(TAG, json.lastErrorText());
        return;
        }

    // To pretty-print, set the EmitCompact property equal to false
    json.put_EmitCompact(false);

    // If bare-LF line endings are desired, turn off EmitCrLf
    // Otherwise CRLF line endings are emitted.
    json.put_EmitCrLf(false);

    // Emit the formatted JSON:
    Log.i(TAG, json.emit());

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}