React Native
React Native
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 React Native Downloads
import { JsonObject } from '@chilkat/react-native'
function chilkatExample() {
const json = new JsonObject();
// 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.
const jsonStr = '{"name": "donut","image":{"fname": "donut.jpg","w": 200,"h": 200},"thumbnail":{"fname": "donutThumb.jpg","w": 32,"h": 32}}';
try {
json.load(jsonStr);
} catch {
console.log(json.lastErrorText);
return;
}
// Get the "image" object.
const imageObj = new JsonObject();
json.objectOf2('image', imageObj);
console.log(`image: fname=${imageObj.stringOf('fname')}, width=${imageObj.intOf('w')}, height=${imageObj.intOf('h')}`);
// Get the "thumbnail" object.
const thumbObj = new JsonObject();
json.objectOf2('thumbnail', thumbObj);
console.log(`thumbnail: fname=${thumbObj.stringOf('fname')}, width=${thumbObj.intOf('w')}, height=${thumbObj.intOf('h')}`);
}