UWSC用Jsonモジュール json2.jsはその都度ファイルから読み出すか、textblockにコピペしとくと良い
call Module_Json.uws
textblock jsonString
{
"foo":{
"bar":"abcde",
"baz":[
123,
456
]
}
}
endtextblock
// 文字列をパース
obj = JSON.Parse(jsonString)
print obj.foo.bar
obj.foo.bar = "vwxyz" // 代入
print obj.foo.bar
print obj.foo.baz.Item(0) // 配列の読み書きはItem()メソッドを使う
obj.foo.baz.Item(0, 789) // 配列の要素に代入する場合は第二引数を渡す
print obj.foo.baz.item(0) // Item()は頭文字を小文字にしてもOK
// オブジェクトの追加
JSON.AddObject(obj.foo, "qux", "( ‘(I¥‘)")
// 文字列に戻す
print JSON.Stringify(obj, 2, TRUE)
module JSON
procedure JSON
ScriptControl = createoleobj("ScriptControl")
with ScriptControl
.Language = "JScript"
.ExecuteStatement(json2)
.ExecuteStatement(jsStatement)
CodeObject = .CodeObject
endwith
fend
dim ScriptControl,CodeObject
function Parse(str)
try
result = CodeObject.JSON.parse(str)
except
result = NOTHING
endtry
fend
function Stringify(json, indent = "", CRLF = FALSE)
try
result = CodeObject.JSON.stringify(json, null, indent)
if CRLF then
result = replace(result, chr(10), "<#CR>")
endif
except
result = EMPTY
endtry
fend
function AddObject(base, name, value)
try
CodeObject.Add(base, name, value)
result = TRUE
except
result = FALSE
endtry
fend
function ReadFromFile(path)
result = EMPTY
if ! fopen(path, F_EXISTS) then exit
fid = fopen(path, F_READ)
str = fget(fid, F_ALLTEXT)
fclose(fid)
result = Parse(str)
fend
function SaveToFile(path, json, indent = "", writemode = F_WRITE8)
result = FALSE
str = Stringify(json, indent)
if str = EMPTY then exit
fid = fopen(path, writemode)
if fid = -1 then exit
fput(fid, str, F_ALLTEXT)
if ! fclose(fid) then exit
result = TRUE
fend
textblock jsStatement
// 配列アクセス用
// 第二引数に値を渡した場合は代入、省略した場合は値取得
Array.prototype.Item = function(i, value)
{
if (! value)
return this[i];
this[i] = value;
}
Array.prototype.item = Array.prototype.Item;
// Object追加用、AddObject()で使う
function Add(obj, name, value)
{
obj[name] = value;
}
endtextblock
endmodule
// ここにjson2.jsの中身をこっぺしとく
textblock json2
endtextblock