Dart Requires Chilkat v11.0.0+
Dart
Swap JSON Objects
See more JSON Examples
Demonstrates how to swap two JSON objects within a JSON document.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final json = CkJsonObject();
json.emitCompact = false;
// Load the following JSON:
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// }
// }
try {
json.loadFile('qa_data/json/people.json');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Swap the positions of Jon and Sara.
var index1 = json.indexOf('Jon');
var index2 = json.indexOf('Sara');
json.swap(index1, index2);
// We have this now:
print(json.emit());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// }
// }
// To swap an inner member:
final jsonSara = CkJsonObject();
json.objectOf2('Sara', jsonSara);
index1 = jsonSara.indexOf('DOB');
index2 = jsonSara.indexOf('country');
jsonSara.swap(index1, index2);
// We now have this:
print(json.emit());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "country": "FR",
// "gender": "female",
// "DOB": "13/05/1982"
// }
// }
}