Help on asdf json module
Sebastiaan Koppe
mail at skoppe.eu
Sun Oct 25 07:27:18 UTC 2020
On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:
> Hi All,
>
> Currently we are testing various json module such as
> "std.json, std_data_json, vibe.data.json and asdf", the below
> code works perfectely while use "std_data_json or
> vibe.data.json" but not working as expected when we use "asdf"
> and throwing the below error, hence request your help on the
> same.
The API's are different. You can't just expect one to work with
the api of the other.
Asdf is a bit more strict in that you have to specify whether you
intent to iterate a json object (with `.byKeyValue()`) or a json
array (with `.byElement()`).
Also the way you fetch values out of it is different. There is
`.get!T(default)` that requires a default value in case it is
empty (undefined), or `cast(T)field` if you assume there is a
value and want an exception throw if not.
----
/+dub.sdl:
dependency "asdf" version="~>0.6.6"
+/
import std.stdio: writeln;
import asdf;
import std.conv: to;
void main()
{
string apidata =
`{"items":[{"name":"T10","hostname":"test1","type":[{"typeValue":"TD,dev at dev.com,DEVt"},{"typeno":"000"}]},{"name":"T11","hostname":"test2","type":[{"typeValue":"TF,qas at qas.com,QAS"},{"typeno":"100"}]},{"name":"T11","hostname":"test3","type":[{"typeValue":"TP,prd at prd.com,PRD"},{"typeno":"101"}]}]}`;
auto jv = parseJson(apidata);
foreach(j; jv["items"].byElement()){
writeln(j["name"].get!string("default"));
}
}
More information about the Digitalmars-d-learn
mailing list