Dart Requires Chilkat v11.0.0+
Dart
JSON: Miscellaneous Operations
See more JSON Examples
Demonstrates a variety of JSON API methods. This example uses the following JSON document:
{
"alphabet": "abcdefghijklmnopqrstuvwxyz",
"sampleData" : {
"pi": 3.14,
"apple": "juicy",
"hungry": true,
"withoutValue": null,
"answer": 42
}
}
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final json = CkJsonObject();
json.emitCompact = false;
// Assume the file contains the data as shown above..
try {
json.loadFile('qa_data/json/sample2.json');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the "sampleData" object:
final sampleData = CkJsonObject();
json.objectOf2('sampleData', sampleData);
// Demonstrate BoolAt and BoolOf
print('hungry: ${sampleData.boolOf('hungry')}');
print('hungry: ${sampleData.boolAt(2)}');
// StringOf returns the value as a string regardless of it's actual type:
print('pi: ${sampleData.stringOf('pi')}');
print('answer: ${sampleData.stringOf('answer')}');
print('withoutValue: ${sampleData.stringOf('withoutValue')}');
print('hungry: ${sampleData.stringOf('hungry')}');
// Demonstrate IsNullOf / IsNullAt
print('withoutValue is null? ${sampleData.isNullOf('withoutValue')}');
print('withoutValue is null? ${sampleData.isNullAt(3)}');
print('apple is null? ${sampleData.isNullOf('apple')}');
print('apple is null? ${sampleData.isNullAt(1)}');
// IntOf
print('answer: ${sampleData.intOf('answer')}');
// SetNullAt, SetNullOf
// Set "pi" to null
sampleData.setNullAt(0);
// Set "answer" to null
sampleData.setNullOf('answer');
// Show the changes:
print(json.emit());
// Restore pi and apple:
sampleData.setNumberAt(0, '3.14');
sampleData.setNumberOf('answer', '42');
// Show the changes:
print(json.emit());
// Add a null value named "afterApple" just after "apple"
sampleData.addNullAt(2, 'afterApple');
// Add a boolean value just after "pi"
sampleData.addBoolAt(1, 'afterPi', false);
// Examine the changes..
print(json.emit());
}