Sample code for 30+ languages & platforms
React Native

JSON: Nested Array

See more JSON Examples

Here we have a JSON object that contains nested arrays. This example demonstrates how to access the contents of the nested arrays.
{
 "numbers" : [ 
    ["even", 2, 4, 6, 8], 
    ["prime", 2, 3, 5, 7, 11, 13] 
  ] }

Chilkat React Native Downloads

React Native
import { JsonArray, JsonObject } from '@chilkat/react-native'

function chilkatExample() {
  const json = new JsonObject();

  // This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars).
  // The presence of whitespace chars for pretty-printing makes no difference to the Load
  // method. 
  const jsonStr = '{ "numbers" : [ ["even", 2, 4, 6, 8], ["prime", 2, 3, 5, 7, 11, 13] ] }';

  try {
    json.load(jsonStr);
  } catch {
    console.log(json.lastErrorText);
    return;
  }

  // Get the value of the "numbers" object, which is an array that contains JSON arrays.
  let outerArray: JsonArray;
  try {
    outerArray = json.arrayOf('numbers');
  } catch {
    console.log('numbers array not found.');
    return;
  }

  const numArrays = outerArray.size;

  for (let i = 0; i < numArrays; i++) {

    const innerArray = outerArray.arrayAt(i);

    // The first item in the innerArray is a string
    console.log(`${innerArray.stringAt(0)}:`);

    const numInnerItems = innerArray.size;
    for (let j = 1; j < numInnerItems; j++) {

      console.log(`  ${innerArray.intAt(j)}`);
    }
  }
}