Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.algorithm;
- import std.array;
- import json;
- void main(string[] argv) {
- // Create a JSON object.
- auto obj = jsonObj();
- // Assign some values to the object.
- obj["foo"] = 3;
- obj["bar"] = null;
- obj["baz"] = "some string";
- obj["sub"] = jsonObj();
- // Iterate over the object.
- foreach(string key, value; obj) {
- writefln("%s : %s", key, value);
- }
- // Use 'in' to check existence.
- assert("sub" in obj);
- // cast a number back from the JSON object again.
- assert(cast(int) obj["foo"] == 3);
- // == is overloaded too.
- assert(obj["baz"] == "some string");
- // We have length
- assert(obj.length == 4);
- // This is cast(bool), and it's true because .length > 0
- assert(obj);
- // A new array. (JSON)
- auto arr = jsonArr();
- // Add on some values.
- arr ~= 3;
- arr ~= 4;
- arr ~= 7;
- assert(arr.length == 3);
- // .arr gets us a reference to the JSON[], which lets us have some fun.
- assert(arr.arr.map!(j => cast(int) j).array == [3, 4, 7]);
- // We can write to the length.
- arr.length = 2;
- // Iterate over the array.
- foreach(size_t i, value; arr) {
- writefln("%s : %s", i, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement