#include "protheus.ch"
user function DNExemplo()
local oJson
local ret
oJson := JsonObject():new()
ret := oJson:fromJson('{"character":"Joaquim", "json":{"Joao":22, "Joana":33}, "array":[4,12,5], "numeric":23, "logic":false, "nil":null}')
if ValType(ret) == "U"
Conout("JsonObject populado com sucesso")
else
Conout("Falha ao popular JsonObject. Erro: " + ret)
endif
u_PrintJson(oJson)
/*
Será impresso:
Label - character
character = Joaquim
Label - numeric
numeric = 23
Label - logic
Label - array
Vetor[
Indice 1
4
Indice 2
12
Indice 3
5
]Vetor
Label - nil
Label - json
*/
oJson:DelName("json")
u_PrintJson(oJson)
/*
Label - character
character = Joaquim
Label - numeric
numeric = 23
Label - logic
Label - array
Vetor[
Indice 1
4
Indice 2
12
Indice 3
5
]Vetor
Label - nil
*/
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
|