Zig Requires Chilkat v11.4.0+
Zig
Example: JsonObject.NewObjectOf method
Demonstrates theNewObjectOf method.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const car = "{\"make\":\"Toyota\",\"model\":\"Camry\",\"year\":2022,\"color\":\"silver\",\"engine\":{\"type\":\"inline-4\",\"fuel\":\"gasoline\",\"horsepower\":203},\"features\":[\"bluetooth\",\"backup camera\",\"adaptive cruise control\",\"lane assist\"],\"isElectric\":false}";
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("test", "abc") catch {};
json.newObjectOf("car", car) catch {
std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
return;
};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Result:
// {
// "test": "abc",
// "car": {
// "make": "Toyota",
// "model": "Camry",
// "year": 2022,
// "color": "silver",
// "engine": {
// "type": "inline-4",
// "fuel": "gasoline",
// "horsepower": 203
// },
// "features": [
// "bluetooth",
// "backup camera",
// "adaptive cruise control",
// "lane assist"
// ],
// "isElectric": false
// }
// }
}