www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - parse json-string - operator overload error

reply zusta <zusta mail.bg> writes:
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.
Dec 01 2010
parent "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> writes:
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.
May 17 2012