Closed woodgear closed 5 years ago
Seems like you have an error in your config.json as this works OK:
<html>
<head>
<title>Test</title>
<script type="text/tiscript">
function getData(path) {
var config = Stream.openString("{foo:42}");
if(config)
{
stdout.println("access file is correct");
var jsonData=null;
try {
//the assign expression and parseData work correct
jsonData = parseData("{}");
stdout.println("config is accesable",config);
// so what happen now?
jsonData = parseData(config);
stdout.println("after parseData");
} catch(e) {
stdout.println("parseData err ",e);
}
if(jsonData){
stdout.printf("JSON data is : %v\n", jsonData);
} else {
stdout.println("No data");
}
config.close();
}else {
stdout.println("could not find file ",path);
}
}
// getData("C:\\Users\\developer\\Desktop\\test_sciter_file\\config.json")
getData("config.json")
</script>
</head>
<body>
<button #t>TEST</button>
</body>
</html>
same result even i change config.json
PS C:\Users\developer\Desktop\test_sciter_file> cat .\config.json
{ name:"John" }
is there someway to make a stream into a string? the toString method just return the file name.
File test.htm:
<html>
<head>
<title>Test</title>
<script type="text/tiscript">
function getData(path) {
var config = Stream.openFile(path,"ru");
if(config)
{
stdout.println("access file is correct");
var jsonData=null;
try {
//the assign expression and parseData work correct
jsonData = parseData("{}");
stdout.println("config is accesable",config);
// so what happen now?
jsonData = parseData(config);
stdout.println("after parseData");
} catch(e) {
stdout.println("parseData err ",e);
}
if(jsonData){
stdout.printf("JSON data is : %v\n", jsonData);
} else {
stdout.println("No data");
}
config.close();
}else {
stdout.println("could not find file ",path);
}
}
// getData("C:\\Users\\developer\\Desktop\\test_sciter_file\\config.json")
getData(__FOLDER__ + "config.json");
</script>
</head>
<body>
<button #t>TEST</button>
</body>
</html>
File config.json in the same folder:
{ name:"John" }
looks ok here. Check if your file is UTF-8 encoded.
As of reading file stream:
function streamToString(stream) {
var out = "";
while(true) {
var line = stream.readln();
if( line === undefined ) break;
out += line + "\n";
}
return out;
}
thanks a lot. the cause of this question is that config.json is been encoded as 'utf8-with-bom'.