Sample code for 30+ languages & platforms
Android™

Iterate over JSON Array containing JSON Objects

See more JSON Examples

Demonstrates how to load a JSON array and iterate over the JSON 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;

    // Loads the following JSON array and iterates over the objects:
    // 
    // [
    // {"tagId":95,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":98,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":101,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":104,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":107,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":110,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":113,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":114,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":111,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":108,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":105,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":102,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":99,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":96,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":97,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":100,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":103,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":106,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":109,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":112,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":115,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":93,"tagDescription":"new tag","isPublic":true},
    // {"tagId":94,"tagDescription":"new tag","isPublic":true},
    // {"tagId":89,"tagDescription":"tag 1","isPublic":true},
    // {"tagId":90,"tagDescription":"tag 2","isPublic":true},
    // {"tagId":91,"tagDescription":"tag private 1","isPublic":false},
    // {"tagId":92,"tagDescription":"tag private 2","isPublic":false}
    // ]

    // Load a file containing the above JSON..
    CkStringBuilder sbJsonArray = new CkStringBuilder();
    success = sbJsonArray.LoadFile("qa_data/json/arraySample.json","utf-8");

    CkJsonArray arr = new CkJsonArray();
    success = arr.LoadSb(sbJsonArray);

    int tagId;
    String tagDescription;
    boolean isPublic;

    int i = 0;
    int count = arr.get_Size();
    CkJsonObject obj;
    while (i < count) {
        obj = arr.ObjectAt(i);
        tagId = obj.IntOf("tagId");
        tagDescription = obj.stringOf("tagDescription");
        isPublic = obj.BoolOf("isPublic");

        Log.i(TAG, "tagId: " + String.valueOf(tagId));
        Log.i(TAG, "tagDescription: " + tagDescription);
        Log.i(TAG, "isPublic: " + String.valueOf(isPublic));
        Log.i(TAG, "--");

        i = i + 1;
        }


  }

  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."
  }
}