user function exemploGJO()
local oJson
local ret
oJson := JsonObject():new()
ret := oJson:fromJson('{"name":{"John":1, "Mary":2}, "age":31, "city":"New York"}')
if ValType(ret) == "U"
Conout("JsonObject populado com sucesso")
else
Conout("Falha ao popular JsonObject. Erro: " + ret)
return
endif
ret := oJson:GetJsonObject("name")
if ValType(ret) == "J"
u_PrintJson(ret)
else
if ValType(ret) <> "U"
conout(cValtochar(ret))
else
conout("falha ao obter JsonObject")
endif
endif
/*
Será impresso:
Label - John
John = 1
Label - Mary
Mary = 2
*/
ret := oJson:GetJsonObject("age")
if ValType(ret) == "J"
u_PrintJson(ret)
else
if ValType(ret) <> "U"
conout(cValtochar(ret))
else
conout("falha ao obter JsonObject")
endif
endif
/*
Será impresso:
31
*/
ret := oJson:GetJsonObject("esto non exist")
if ValType(ret) == "J"
u_PrintJson(ret)
else
if ValType(ret) <> "U"
conout(cValtochar(ret))
else
conout("falha ao obter JsonObject")
endif
endif
/*
Será impresso:
falha ao obter JsonObject
*/
FreeObj(oJson)
return
user function PrintJson(jsonObj)
local i, j
local names
local lenJson
local item
lenJson := len(jsonObj)
if lenJson > 0
for i := 1 to lenJson
u_PrintJson(jsonObj[i])
next
else
names := jsonObj:GetNames()
for i := 1 to len(names)
conout("Label - " + names[i])
item := jsonObj[names[i]]
if ValType(item) == "C" .or. ValType(item) == "N"
conout( names[i] + " = " + cvaltochar(jsonObj[names[i]]))
else
if ValType(item) == "A"
conout("Vetor[")
for j := 1 to len(item)
conout("Indice " + cValtochar(j))
if ValType(item[j]) == "J"
u_PrintJson(item[j])
else
conout(cvaltochar(item[j]))
endif
next j
conout("]Vetor")
endif
endif
next i
endif
return
|