React Native Requires Chilkat v11.0.0+
React Native
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 React Native Downloads
import { JsonObject } from '@chilkat/react-native'
function chilkatExample() {
const json = new JsonObject();
json.emitCompact = false;
// Assume the file contains the data as shown above..
try {
json.loadFile('qa_data/json/sample2.json');
} catch {
console.log(json.lastErrorText);
return;
}
// Get the "sampleData" object:
const sampleData = new JsonObject();
json.objectOf2('sampleData', sampleData);
// Demonstrate BoolAt and BoolOf
console.log(`hungry: ${sampleData.boolOf('hungry')}`);
console.log(`hungry: ${sampleData.boolAt(2)}`);
// StringOf returns the value as a string regardless of it's actual type:
console.log(`pi: ${sampleData.stringOf('pi')}`);
console.log(`answer: ${sampleData.stringOf('answer')}`);
console.log(`withoutValue: ${sampleData.stringOf('withoutValue')}`);
console.log(`hungry: ${sampleData.stringOf('hungry')}`);
// Demonstrate IsNullOf / IsNullAt
console.log(`withoutValue is null? ${sampleData.isNullOf('withoutValue')}`);
console.log(`withoutValue is null? ${sampleData.isNullAt(3)}`);
console.log(`apple is null? ${sampleData.isNullOf('apple')}`);
console.log(`apple is null? ${sampleData.isNullAt(1)}`);
// IntOf
console.log(`answer: ${sampleData.intOf('answer')}`);
// SetNullAt, SetNullOf
// Set "pi" to null
sampleData.setNullAt(0);
// Set "answer" to null
sampleData.setNullOf('answer');
// Show the changes:
console.log(json.emit());
// Restore pi and apple:
sampleData.setNumberAt(0, '3.14');
sampleData.setNumberOf('answer', '42');
// Show the changes:
console.log(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..
console.log(json.emit());
}