PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • JSON and WEBDEV
  • Getting information in JSON format
  • Example for using the JSONExecute function
  • Example for using the JSONExecuteExternal function
  • Creating pages that return JSON data
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
JSON (JavaScript Object Notation) is a light format for exchanging data. Based on JavaScript, JSON is a text format independent of any other language.
JSON is based on two structures:
  • a set of name/value couples, interpreted in WLanguage by structures.
  • a list of organized values, interpreted in WLangage by arrays.
JSON and WEBDEV
WEBDEV is used to:
  • get information in JSON format in a WEBDEV site.
  • create WEBDEV pages that return information in JSON format. You have the ability to create a site that provides JSON services (a site for monitoring packages for example).
Getting information in JSON format
To get information in JSON format, WEBDEV allows you to run a page that returns information in JSON format. Two WLangage functions are available:
JSONExecuteCalls a server URL of the same domain that returns data in JSON format (JavaScript Object Notation).
JSONExecuteExternalCalls an external server URL that returns data in JSON format (JavaScript Object Notation). The data is processed in a specific procedure.
These functions are browser functions. The data received is processed by a browser code.

Example for using the JSONExecute function

The following code is used to run an AWP page in order to get the list of contacts in JSON format.
The steps are as follows:
  1. Declaring a dynamic object. This dynamic object will contain the result in JSON format.
    MyContacts is dynamic object
  2. Call to JSONExecute:
    MyContacts = JSONExécute(FolderWeb()+"FR/PAGE_Object.awp?id=12")
  3. Processing the JSON data: This process is performed in browser code.
    Let's take a simple example: the JSON data is returned in the following format:
    {id: 12,
    list: [
    {lastname: "smith", firstname: "john"},
    {lastname: "doe", firstname: "mary"},
    {lastname: "martin", firstname: "laura"}]
    }

    You can for example:
    • retrieve the value of a member of the dynamic object. For example:
      MyContacts:id
    • retrieve the different elements from a list of values. For example:
      FOR i = 1 _TO_ Dimension(MyContacts:list)
      ListAdd(LIST_List_of_contacts, ...
      MyContacts:list[i]:lastname + "  " + ...
      MyContacts:list[i]:firstname)
      END

Example for using the JSONExecuteExternal function

The principle for using JSONExecuteExternal is the same as the principle for using JSONExecute.
In this case, the page used to retrieve the JSON data is not found in the same domain. The security rules of Internet impose to use a callback function to process the result in asynchronous way. Therefore, JSONExecuteExternal allows you to specify the name of the procedure run to process the JSON data.
Example: In this example, an AWP page is called and the data is processed in the FunctionResponse procedure:
JSONExecuteExternal("http://MySite/MySite_WEB/US/PAGE_Object.awp?id=12", ...
"JsonCallback", FunctionResponse)
Creating pages that return JSON data
WEBDEV gives you the ability to create a site that provides JSON services.
The pages used to get the JSON data must return a character string of a specific format. These pages can be AWP pages or PHP pages.
Case of a page called by JSONExecute: The following code is used to return an identifier and a list of values:
sObject is string = [
{id: 12,
list:  [
{lastname: "smith", firstname: "john"},
{lastname: "doe", firstname: "mary"},
{lastname: "martin", firstname: "laura"}]
}
]
// StringToUTF8 is used to manage the accented characters
StringDisplay(StringToUTF8(sObject))
StringDisplay is used to return the JSON information. StringToUTF8 is used to return a string in UTF8 format. This last function is mandatory to get a valid format.
Case of a page called by JSONExecuteExternal: In addition to the code used to manage the JSON element to return, the name of the procedure that will be used to process the data must be specified in the data returned. The name of this procedure is indicated in parameter on the URL of the page.
The following example is used to manage the call with JSONExecute and JSONExecuteExternal. For an external call, you must check the presence of a specific parameter found on the URL. This parameter was supplied to the user of the JSON service.
sObject is string = [
{id: 12,
list:  [
{lastname: "smith", firstname: "john"},
{lastname: "doe", firstname: "mary"},
{lastname: "martin", firstname: "laura"}]
}
]
// Manage an external call.
// Checks the presence of a procedure name on the URL
IF PageParameter("JsonCallback") <> "" THEN
sObject = PageParameter("JsonCallback") + "("+ sObject + ");"
END

// StringToUTF8 is used to manage the accented characters
StringDisplay(StringToUTF8(sObject))
In order for the users of WEBDEV sites to be able use the JSON services, all you have to do is inform these users of the parameters to specify in the address of the page to start.
Note: See a specific documentation for more details.
Minimum required version
  • Version 14
Comments
Exemplo de leitura de arquivo json
gsPerguntasJson is ANSI string = fLoadText( fDataDir()+"/questoes.json" )
gsPerguntasJson = UTF8ToAnsi(gsPerguntasJson)

JsonStru is Structure
pergunta is ANSI string
resp_a is ANSI string
resp_b is ANSI string
resp_c is ANSI string
resp_d is ANSI string
resp_e is ANSI string
resp_f is ANSI string
resp_g is ANSI string
resposta is ANSI string
explicacao is ANSI string
END

gstDadosJson is array of JsonStru
Deserialize( gstDadosJson , gsPerguntasJson , psdJSON)
FOR EACH st_linha_json OF gstDadosJson
Info(st_linha_json.explicacao)
END
BOLLER
Aug. 11 2017
Exemplo Deserialise - Json
Aula 1050 - WinDev - Json 001/... - Deserialise

// Nessa aula vou mostrar como retirar campos do Json

SAI_TextoJson=[
{"cep":"93410130","tipoDeLogradouro":"Rua","logradouro":"Paraiba","bairro":"Patria Nova","cidade":"Novo Hamburgo","estado":"RS"}
]


_cep_ is Structure
cep is string
tipoDeLogradouro is string
logradouro is string
bairro is string
cidade is string
estado is string
END
Estrutura_json is _cep_

Deserialize(Estrutura_json, SAI_TextoJson, psdJSON)

EDT_rua=Estrutura_json.logradouro
EDT_bairro=Estrutura_json.bairro
EDT_Cidade=Estrutura_json.cidade
EDT_Estado=Estrutura_json.estado
EDT_TipodeLogradouro=Estrutura_json.tipoDeLogradouro

//Blog com Video e Exemplo

http://windevdesenvolvimento.blogspot.com.br/2017/01/aula-1050-windev-json-001-deserialise.html

https://www.youtube.com/watch?v=-YGvpvKDgB8

http://forum.pcsoft.fr/pt-BR/pcsoft.br.windev/2089-aula-1050-windev-json-001-deserialise/read.awp
De matos
Jan. 28 2017