View mode: basic / threaded / horizontal-split · Log in · Help
December 01, 2010
parse json-string - operator overload error
Hey guys,

I'm trying to read a JSON-formated file. The parsing of the file seems to be
correct, but I always get the following error:

"Error: no [] operator overload for type JSONValue".

For testing purposes, I also tried the code of
http://www.digitalmars.com/d/archives/digitalmars/D/std.json_API_improvement_
-_Request_for_code_review_117261.html#N117302 without success (same error).

My currently code looks like this:

import std.stdio : writeln;
import std.json;
import std.stream;
import std.conv;

void main(string[] args) {

	File f = new File("test.json", FileMode.In);
	string content = to!(string)(f.readString(f.available()));
	JSONValue t = parseJSON(content);
	writeln(t["test"]);
	f.close();

}

I hope anyone can help to find out what's wrong with the code above.
May 17, 2012
Re: parse json-string - operator overload error
On Wednesday, 1 December 2010 at 08:17:37 UTC, zusta wrote:
> Hey guys,
>
> I'm trying to read a JSON-formated file. The parsing of the 
> file seems to be
> correct, but I always get the following error:
>
> "Error: no [] operator overload for type JSONValue".
>
> For testing purposes, I also tried the code of
> http://www.digitalmars.com/d/archives/digitalmars/D/std.json_API_improvement_
> -_Request_for_code_review_117261.html#N117302 without success 
> (same error).
>
> My currently code looks like this:
>
> import std.stdio : writeln;
> import std.json;
> import std.stream;
> import std.conv;
>
> void main(string[] args) {
>
> 	File f = new File("test.json", FileMode.In);
> 	string content = to!(string)(f.readString(f.available()));
> 	JSONValue t = parseJSON(content);
> 	writeln(t["test"]);
> 	f.close();
>
> }
>
> I hope anyone can help to find out what's wrong with the code 
> above.

Assuming the file reading was not the fault:

	string content = "{ \"test\": \"1\"  }";
	JSONValue[string] t = parseJSON(content).object;
	writeln(t["test"].str);

This is one way to access the data in the file.
Top | Discussion index | About this forum | D home