Thread overview | |||||
---|---|---|---|---|---|
|
April 13, 2020 How to detect whethere if a JSON node exists | ||||
---|---|---|---|---|
| ||||
In the following code, I want to process an json array (returned by the value of "posts") that might or might not have "com" key. If a "com" key does not exist, I want to ignore that item in the json array. uint[string] wordTable; const auto j = parseJSON(get(link)); foreach (node; j["posts"].array()) { import std.stdio : writeln; import std.utf : decode; import std.algorithm: splitter; foreach(word; getStr(node["com"].str()).splitter(' ')) { // here! if (word != "") wordTable.require(word, 0)++; } } Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed? |
April 13, 2020 Re: How to detect whethere if a JSON node exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adnan | On Monday, 13 April 2020 at 02:20:39 UTC, Adnan wrote:
> Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed?
Try:
if("com" in node) {
}
|
April 13, 2020 Re: How to detect whethere if a JSON node exists | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 13 April 2020 at 02:22:33 UTC, Adam D. Ruppe wrote:
> On Monday, 13 April 2020 at 02:20:39 UTC, Adnan wrote:
>> Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed?
>
> Try:
>
> if("com" in node) {
>
> }
Came back here to say that I found exactly this solution. Thanks.
|
Copyright © 1999-2021 by the D Language Foundation