Árvore de páginas

Carregando...

Você está vendo a versão antiga da página. Ver a versão atual.

Comparar com o atual Ver Histórico da Página

« Anterior Versão 13 Próxima »

Retorna um objeto do tipo JsonObject que seja o conteúdo de uma propriedade em outro objeto JsonObject.

Sintaxe

JsonObject:GetJsonObject( cPropriedade )

Parâmetros

NomeTipoDescriçãoObrigatórioReferência
cPropriedadecaractereNome da propriedade cujo valor será retornadoX


Retorno

Nome

Tipo

Descrição

oJsonObj

JsonObject

Retorna um objeto JsonObject conteúdo da propriedade informada em caso de sucesso ou NIL em caso de erro.


Exemplo

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

  if ValType(jsonObj) <> "J"
    conout("PrintJson --> objeto recebido nao é um JsonObject")
    return
  endif

  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))
            u_PrintJson(item[j])
          next j
          conout("]Vetor")
        endif
      endif
    next i
  endif
return


Observações:

Disponível em build igual ou superior a 17.2.1.0

  • Sem rótulos