Thread overview |
---|
November 08, 2020 asdf get first value from a json string. | ||||
---|---|---|---|---|
| ||||
Hi All, Request your help on how to get the first value of "type" from the below json, the expected output required is as below, {"characteristicValue":"TT,TT@dev.com,DEV"} output1: TT output2: TT@dev.com Code: /+dub.sdl: dependency "asdf" version="~>0.6.6" +/ import std; import asdf; void main() { string data = `{"items":[{ "hostname":"test01", "type":[ {"characteristicValue":"TT,TT@dev.com,DEV"}, {"characteristicValue":"000"} ]}]}`; foreach (ref j; parseJson(data)["items"].byElement()) { writeln(j["type"]) ; } } From Vino.B |
November 08, 2020 Re: asdf get first value from a json string. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | On Sunday, 8 November 2020 at 16:30:40 UTC, Vino wrote: > Request your help on how to get the first value of "type" from the below json, the expected output required is as below, You need a data structure to work with, eg: static struct S { string characteristicValue; } foreach (ref j; parseJson(data)["items"].byElement()) { auto sArr = j["type"].deserialize!(S[]); writefln("value: %s", s[0].characteristicValue); } Then split your S.characteristicValue into tokens with std.array.split |
November 08, 2020 Re: asdf get first value from a json string. | ||||
---|---|---|---|---|
| ||||
Posted in reply to frame | On Sunday, 8 November 2020 at 19:29:39 UTC, frame wrote:
> On Sunday, 8 November 2020 at 16:30:40 UTC, Vino wrote:
>> Request your help on how to get the first value of "type" from the below json, the expected output required is as below,
>
> You need a data structure to work with, eg:
>
> static struct S {
> string characteristicValue;
> }
>
> foreach (ref j; parseJson(data)["items"].byElement()) {
> auto sArr = j["type"].deserialize!(S[]);
> writefln("value: %s", s[0].characteristicValue);
> }
>
> Then split your S.characteristicValue into tokens with std.array.split
writefln("value: %s", sArr[0].characteristicValue);
|
November 09, 2020 Re: asdf get first value from a json string. | ||||
---|---|---|---|---|
| ||||
Posted in reply to frame | On Sunday, 8 November 2020 at 19:31:50 UTC, frame wrote:
> On Sunday, 8 November 2020 at 19:29:39 UTC, frame wrote:
>> On Sunday, 8 November 2020 at 16:30:40 UTC, Vino wrote:
>>> Request your help on how to get the first value of "type" from the below json, the expected output required is as below,
>>
>> You need a data structure to work with, eg:
>>
>> static struct S {
>> string characteristicValue;
>> }
>>
>> foreach (ref j; parseJson(data)["items"].byElement()) {
>> auto sArr = j["type"].deserialize!(S[]);
>> writefln("value: %s", s[0].characteristicValue);
>> }
>>
>> Then split your S.characteristicValue into tokens with std.array.split
>
> writefln("value: %s", sArr[0].characteristicValue);
Hi,
Thank you very much, your solution resolved the issue.
|
Copyright © 1999-2021 by the D Language Foundation