Json Help

Christian Köstlin christian.koestlin at gmail.com
Tue Sep 12 08:56:45 UTC 2023


On 10.09.23 13:06, Vino wrote:
> Hi All,
> 
>    Request your help on the below code,I am trying to convert the below 
> string to json and it always throws the error, if the below can be 
> accomplished with any other json package even that is fine, I tired only 
> the std.json package.
> 
> Test Program: Works
> ````
> import std.json;
> import std.stdio: writeln;
> 
> void main () {
> 
> auto sadrsJson = parseJSON(`{ "TestHost": { "Building1": { "RackNo": 
> 123}}}`);
> writeln(sadrsJson);
> 
> }
> ```
> Program: Does not Work
> ```
> import std.json;
> import std.stdio;
> import std.container;
> import std.socket: Socket;
> import std.stdio: writeln;
> 
> void main () {
> 
> string host = Socket.hostName;
> string registry = "Test"
> 
> Array!string output;
> Array!string errors;
> 
> output.insertBack("TESTOUTPUT");
> errors.insertBack("TESTERRORS");
> 
> auto sadrsJson = parseJSON({ host: { registry: { "OUTPUT": output[], 
> "ERROR": errors[]}}});
> writeln(sadrsJson);
> 
> }
> 
> From,
> Vino.B
`parseJSON` takes a string as arguments and tries to parse it as JSON.
The first example works, because your argument is a string and in JSON 
format.

The argument in the second example is not a string, and if it would be a 
string, its not JSON (e.g. `host:` is not `"host":`)...

Kind regards,
Christian



More information about the Digitalmars-d-learn mailing list