Sample code for 30+ languages & platforms
Dart

JSON: Nested Objects

See more JSON Examples

Here we have a JSON object that contains nested JSON objects. This example demonstrates how to access the contents of the nested objects.
{
  "name": "donut",
  "image":
    {
    "fname": "donut.jpg",
    "w": 200,
    "h": 200
    },
  "thumbnail":
    {
    "fname": "donutThumb.jpg",
    "w": 32,
    "h": 32
    }
}

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final json = CkJsonObject();

  // 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. 
  final jsonStr = '{"name": "donut","image":{"fname": "donut.jpg","w": 200,"h": 200},"thumbnail":{"fname": "donutThumb.jpg","w": 32,"h": 32}}';

  try {
    json.load(jsonStr);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the "image" object.
  final imageObj = CkJsonObject();
  json.objectOf2('image', imageObj);

  print('image: fname=${imageObj.stringOf('fname')}, width=${imageObj.intOf('w')}, height=${imageObj.intOf('h')}');

  // Get the "thumbnail" object.
  final thumbObj = CkJsonObject();
  json.objectOf2('thumbnail', thumbObj);

  print('thumbnail: fname=${thumbObj.stringOf('fname')}, width=${thumbObj.intOf('w')}, height=${thumbObj.intOf('h')}');
}