#include "protheus.ch"
user function tstJsonObjectexemploJson()
local wrk
wrk
local oJson
local cTextJson
local ret
oJson := JsonObject():newNew()
wrk['FABRICA'] := 'Wemagete' //cria automaticamente uma propriedade de nome FABRICA
wrk['NOME'] := 'Fissori' //cria automaticamente uma propriedade de nome NOME
Conout(wrk['FABRICA'])
Conout(wrk:nome)
FreeObj(wrk) cTextJson := '{"itens":[{"joao":"maria","josé":"joana","joaquim":"joaquina","juscelino":"joice"},{"limao":"verde","banana":"amarelo","maça":"vermelho","amora":"roxo"}]}'
ret := oJson:FromJson(cTextJson)
if ValType(ret) == "C"
conout("Falha ao transformar texto em objeto json. Erro: " + ret)
return
endif
u_PrintJson(oJson)
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"
conout( names[i] + " = " + cvaltochar(jsonObj[names[i]]))
else
if ValType(item) == "A"
conout("Vetor[")
for j := 1 to len(item)
conout("Indice " + cValtochar(j))
u_PrintJson(item[j])
next j
conout("]Vetor")
endif
endif
next i
endif
return
|