Converting JSONValue to AssociativeArray.
Ali Çehreli
acehreli at yahoo.com
Mon Aug 1 22:53:02 UTC 2022
On 8/1/22 15:47, Steven Schveighoffer wrote:
You beat me to it. I used .object and .str:
import std;
void main()
{
JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby":
"Programming" }`);
writefln("%s", data);
// This already sees the data as an AA but the type is
JSONValue[string]":
auto o = data.object;
static assert(is(typeof(o) == JSONValue[string]));
writeln("Accessing the data with .str:");
foreach (key, jsonValue; o) {
writeln(" ", key, ": ", jsonValue.str);
}
// If that much is not sufficient, you can convert it to a
// proper AA like this:
auto aa_data = o
.byKeyValue
.map!(kv => tuple(kv.key, kv.value.str))
.assocArray;
static assert(is(typeof(aa_data) == string[string]));
writeln("Now it's string[string]:");
writefln(" %s", aa_data);
}
Ali
More information about the Digitalmars-d-learn
mailing list