Sample code for 30+ languages & platforms
React Native

JSON Iterate Members

See more JSON Examples

Demonstrates how to loop over the immediate members of a JSON object.

Chilkat React Native Downloads

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

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

  const jsonStr = '{ "id": 1, "name": "A green door", "tags": ["home", "green"], "price": 125 }';

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

  const numMembers = json.size;
  for (let i = 0; i < numMembers; i++) {

    const name = json.nameAt(i);
    const value = json.stringAt(i);

    console.log(`${name}: ${value}`);

    const iValue = json.intAt(i);
    console.log(`${name} as integer: ${iValue}`);
  }

  // Note: The StringAt method returns the value as a string regardless of the type.
  // If the value is a JSON array (such as for ["home", "green"]), then the JSON encoding
  // of the entire array is returned.

  // The IntAt method returns the value as an integer.  If the value does not convert to 
  // an integer, then 0 is returned.
}