Re: FXJSON
Jeroen van der Zijp <[email protected]>
| Newsgroups | gmane.comp.lib.fox-toolkit.user |
|---|---|
| Organization | FOX Toolkit |
| Message-ID | <[email protected]> |
All you need to do to read .json file is:
FXJSONFile json;
FXVariant var;
if(json.open("myfile.json",FXJSON::Load)){
FXJSON::Error loaderr=json.load(var);
if(loaderr==FXJSON::ErrOK){
// Success!
....
}
else{
fxmessage("Error: %s:%d:%d: %s\n","myfile.json",json.getLine(),json.getColumn(),FXJSON::getError(loaderr));
}
json.close();
}
else{
fxwarning("Error: unable to open: \"%s\" for reading.\n","myfile.json");
}
If successful, FXVariant var will contain the contents of
the json file.
If the contents of the json file was something like:
{
"data" : [1, 2, 3, "string"]
}
then you can read it as:
FXdouble D=var["data"][2];
FXString S=var["data"][3];
The value of D should be equal to 3.0, and the value of S should be "string".
To write a json file, you'd first populate your top-level variant with the
data of your choice:
FXVariant var;
var["name"]="Rick Astley";
var["status"]="rolled";
var["where"][0]="London";
var["where"][1]="Paris";
var["where"][2]["lat"]=20.0;
var["where"][2]["lon"]=30.0;
FXJSONFile json;
json.open("rickrolocations.json",FXJSON::Save)){
json.save(var);
json.close();
}
which should produce a json file:
{
"name" : "Rick Astley",
"status" : "rolled",
"where" : [
"London,
"Paris",
{
"lat" : 20.0,
"lon" : 30.0
}
]
}
The precise layout of the text depends on the various pretty-printing
options you select;
FYI, FXJSON and FXJSONFile don't like you to use their copy constructor or
assignment operators; that's probably why you see compile errors.
Regards,
-- JVZ
--
+----------------------------------------------------------------------------+
| Copyright (C) 19:40 10/30/2021 Jeroen van der Zijp. All Rights Reserved. |
+----------------------------------------------------------------------------+