Zig Requires Chilkat v11.0.0+
Zig
Load JSON Array from HTTP Response Body
Demonstrates how to load the HTTP response body directly into a JsonArray.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 http = try chilkat.Http.init();
defer http.deinit();
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpStr("GET", "https://www.chilkatsoft.com/exampledata/sampleArray.json", "", "", "", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// A JSON array is JSON that begins with "[" and ends with "]"
const jarr = try chilkat.JsonArray.init();
defer jarr.deinit();
// If we wish to transfer (instead of copy) the JSON from the HttpResponse to the JsonArray, then add the keyword "TakeResponseBody" to UncommonOptions
// This could save memory for extremely large JSON responses.
resp.setUncommonOptions("TakeResponseBody");
resp.getBodyJarr(jarr) catch {};
std.debug.print("{s}\n", .{try jarr.emit(alloc)});
// Note: If UncommonOptions contained "TakeResponseBody", then the response BodyStr will now be empty:
std.debug.print("----\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}