Page 1 of 1

How to Decode Json From web service [ to Mr. Rao ]

Posted: Tue May 31, 2016 11:22 am
by fafi
Helloo Mr. Rao
Please test !

Code: Select all

#include "fivewin.ch"

function Main()
local odoc  := CreateObject( "MSXML2.DOMDocument" )
local ohttp := CreateObject( "MSXML2.XMLHTTP" )
ohttp:Open( "GET" ,"http://www.w3schools.com/angular/customers.php", .F. )
ohttp:SetRequestHeader( "Accept"        , "application/xml")
ohttp:SetRequestHeader( "Content-Type" ,"application/json" )
oDoc:async := .f.
oDoc:LoadXml('<?xml version=""1.0"" encoding=""utf-8""?>')
ohttp:Send(oDoc:xml)
strret := alltrim(ohttp:responseText)
?strret
return nil

 
and how to save this return value records to database
Thank you

Best Regards
Fafi

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Tue May 31, 2016 8:05 pm
by Antonio Linares

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Wed Jun 01, 2016 9:23 am
by nageswaragunupudi
Made additions to your program to parse Json data and view/save.
Many users of the forum know the built-in functions of (x)Harbour for json.
Json encoding and decoding is so simple that I prefer to use my own conversion.
Here it is:

Code: Select all

#include "fivewin.ch"

function Main()

   local odoc  := CreateObject( "MSXML2.DOMDocument" )
   local ohttp := CreateObject( "MSXML2.XMLHTTP" )
   local strret

   ohttp:Open( "GET" ,"http://www.w3schools.com/angular/customers.php", .F. )
   ohttp:SetRequestHeader( "Accept"        , "application/xml")
   ohttp:SetRequestHeader( "Content-Type" ,"application/json" )
   oDoc:async := .f.
   oDoc:LoadXml('<?xml version=""1.0"" encoding=""utf-8""?>')
   ohttp:Send(oDoc:xml)
   strret := alltrim(ohttp:responseText)

   ? strret

   StrToHashArray( StrRet )

return nil

function StrToHashArray( cStr )

   local hHash, aHash

   cStr  := CharRepl( "[]", RangeRem( 1, 31, cStr ), "{}" )
   do while '" :' $ cStr
      StrTran( cStr, '" :', '":' )
   enddo
   cStr  := StrTran( cStr, '":', '"=>' )

   hHash := &cStr
   aHash := HGetValueAt( hHash, 1 )
   xbrowser aHash setup ( obrw:autofit(), oBrw:bRClicked := { |r,c,f,o| o:ToDbf( "download.dbf", nil, nil, .t. ) } )

return nil

 
In case you are using an older version of FWH, you may remove "obrw:autofit()," from the above code.

Result of ? strret: (your program)
Image

This text is converted into an array of Hashes, aHash and displayed in xbrowse:
Image

Right-clicking the browse lets you save the data to "download.dbf"
This is the downloaded data in download.dbf
Image

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Thu Jun 02, 2016 1:24 am
by nageswaragunupudi
Saving to DBF from an array of Hashes:

We can also directly save data from an array of hashes to dbf, using FW_HashToRec( hHash, [fieldlist] ) function.

Code: Select all

AEval( aHash, { |h| DbAppend(), FW_HashToRec( h ) } )
Assuming we already know the field names and structure in advance, in the above case we can directly download the data into a dbf like this:

Code: Select all

#include "fivewin.ch"

REQUEST DBFCDX

function Main()

   local odoc  := CreateObject( "MSXML2.DOMDocument" )
   local ohttp := CreateObject( "MSXML2.XMLHTTP" )
   local strret

   ohttp:Open( "GET" ,"http://www.w3schools.com/angular/customers.php", .F. )
   ohttp:SetRequestHeader( "Accept"        , "application/xml")
   ohttp:SetRequestHeader( "Content-Type" ,"application/json" )
   oDoc:async := .f.
   oDoc:LoadXml('<?xml version=""1.0"" encoding=""utf-8""?>')
   ohttp:Send(oDoc:xml)
   strret := alltrim(ohttp:responseText)

   ? StrRet

//   StrToHashArray( StrRet )
   SaveToDBF( StrRet )

return nil

function SaveToDBF( cStr )

   local hData

   cStr  := CharRepl( "[]", RangeRem( 1, 31, cStr ), "{}" )
   do while '" :' $ cStr
      StrTran( cStr, '" :', '":' )
   enddo
   cStr  := StrTran( cStr, '":', '"=>' )

   hData := &cStr

   if File( "download.dbf" )
      USE download.dbf NEW ALIAS DST EXCLUSIVE VIA "DBFCDX"
   else
      DBCREATE( "download.dbf", { { "NAME",    'C', 40, 0 }, ;
                                  { "CITY",    'C', 40, 0 }, ;
                                  { "COUNTRY", 'C', 40, 0 } }, ;
                "DBFCDX", .t., "DST" )
   endif

   AEval( HGetValueAt( hData, 1 ), { |h| DBAPPEND(), FW_HashToRec( h ) } )

   XBROWSER

   CLOSE DST

return nil
 

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Fri Jun 03, 2016 3:59 am
by fafi
:D :D
Terimakasih Mr. Rao ( Indonesian )
Thank you so much

Best Regars
Fafi

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Mon Dec 05, 2016 7:41 pm
by TOTOVIOTTI
Dear Rao:

I'm trying to use your example to pass my Json file to an array, but I encounter the
BASE/1003 error. Variable does not exist: null

How can I solve this problem?
Thank you very much!
Roberto

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Mon Jan 18, 2021 12:34 pm
by Silvio.Falconi
nageswaragunupudi wrote:Saving to DBF from an array of Hashes:

We can also directly save data from an array of hashes to dbf, using FW_HashToRec( hHash, [fieldlist] ) function.

Code: Select all

AEval( aHash, { |h| DbAppend(), FW_HashToRec( h ) } )
Assuming we already know the field names and structure in advance, in the above case we can directly download the data into a dbf like this:

Code: Select all

#include "fivewin.ch"

REQUEST DBFCDX

function Main()

   local odoc  := CreateObject( "MSXML2.DOMDocument" )
   local ohttp := CreateObject( "MSXML2.XMLHTTP" )
   local strret

   ohttp:Open( "GET" ,"http://www.w3schools.com/angular/customers.php", .F. )
   ohttp:SetRequestHeader( "Accept"        , "application/xml")
   ohttp:SetRequestHeader( "Content-Type" ,"application/json" )
   oDoc:async := .f.
   oDoc:LoadXml('<?xml version=""1.0"" encoding=""utf-8""?>')
   ohttp:Send(oDoc:xml)
   strret := alltrim(ohttp:responseText)

   ? StrRet

//   StrToHashArray( StrRet )
   SaveToDBF( StrRet )

return nil

function SaveToDBF( cStr )

   local hData

   cStr  := CharRepl( "[]", RangeRem( 1, 31, cStr ), "{}" )
   do while '" :' $ cStr
      StrTran( cStr, '" :', '":' )
   enddo
   cStr  := StrTran( cStr, '":', '"=>' )

   hData := &cStr

   if File( "download.dbf" )
      USE download.dbf NEW ALIAS DST EXCLUSIVE VIA "DBFCDX"
   else
      DBCREATE( "download.dbf", { { "NAME",    'C', 40, 0 }, ;
                                  { "CITY",    'C', 40, 0 }, ;
                                  { "COUNTRY", 'C', 40, 0 } }, ;
                "DBFCDX", .t., "DST" )
   endif

   AEval( HGetValueAt( hData, 1 ), { |h| DBAPPEND(), FW_HashToRec( h ) } )

   XBROWSER

   CLOSE DST

return nil
 


Nages this test on Windows Seven Not run
make me this error
Application
===========
Path and name: C:\Work\Errori\CREATE_OBJE_OLE\test.Exe (32 bits)
Size: 4,072,448 bytes
Compiler version: Harbour 3.2.0dev (r1904111533)
FiveWin version: FWH 20.12
C compiler version: Borland/Embarcadero C++ 7.0 (32-bit)
Windows version: 6.1, Build 7600

Time from start: 0 hours 0 mins 0 secs
Error occurred at: 01/18/21, 13:33:57
Error description: (DOS Error -2147352567) WINOLE/1007 Accesso negato.
(0x80070005): msxml3.dll
Args:
[ 1] = C

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Mon Jan 18, 2021 1:42 pm
by karinha
https://i.imgur.com/WTKQrTg.png

Image

Code: Select all

#include "fivewin.ch"
#include "directry.ch"
#include "fileio.ch"

#define URL_CONSULTA          "http://www.w3schools.com/angular/customers.php"

FUNCTION Main()

   LOCAL oServer, sTrRet

   /*
   ohttp:Open( "GET" ,"http://www.w3schools.com/angular/customers.php", .F. )
   ohttp:SetRequestHeader( "Accept"        , "application/xml")
   ohttp:SetRequestHeader( "Content-Type" ,"application/json" )
   oDoc:async := .f.

   oDoc:LoadXml('<?xml version=""1.0"" encoding=""utf-8""?>')

   //ohttp:Send(oDoc:xml)
   oDoc:Send()
   */

   #ifdef __XHARBOUR__  // xHarbour

      Try

         oServer := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )

      Catch

         MsgInfo( 'Erro na Criação do Serviço' )

         RETURN NIL

      End

   #else

      Try

         oServer := win_OleCreateObject( "MSXML2.ServerXMLHTTP.5.0" )

      Catch

         MsgInfo( 'Erro na Criação do Serviço!', 'Atenção!' )

         RETURN NIL

      End

   #endif

   Try

      oServer:Open( "GET", URL_CONSULTA, .F. )
      oServer:SetRequestHeader( "Content-Type", "application/x-www-form-urlencoded" )
      oServer:SetRequestHeader( "Connection", "keep-alive" )
      oServer:Send()

      oServer:WaitForResponse( 10000 )

   Catch

      MsgInfo( 'Erro na conexão com o site!', 'Atenção!' )

      RETURN NIL

   End

   sTrRet := alltrim( oServer:responseText )

   ? sTrRet

   StrToHashArray( sTrRet )

RETURN nil

FUNCTION StrToHashArray( cStr )

   LOCAL hHash, aHash

   cStr  := CharRepl( "[]", RangeRem( 1, 31, cStr ), "{}" )

   DO WHILE '" :' $ cStr

      SYSREFRESH()

      StrTran( cStr, '" :', '":' )

   ENDDO

   cStr  := StrTran( cStr, '":', '"=>' )

   hHash := &cStr
   aHash := HGetValueAt( hHash, 1 )

   xbrowser aHash setup ( obrw:autofit(), oBrw:bRClicked := { |r, c, f, o| o:ToDbf( "download.dbf", nil, nil, .T. ) } )

RETURN nil
 

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Mon Jan 18, 2021 2:21 pm
by Silvio.Falconi
ok but this on win 10, i tried on win 7 and not run ok

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Mon Jan 18, 2021 2:47 pm
by karinha
Silvio.Falconi wrote:ok but this on win 10, i tried on win 7 and not run ok
https://i.imgur.com/3RrQB3X.png

Image

Re: How to Decode Json From web service [ to Mr. Rao ]

Posted: Thu Jan 21, 2021 8:35 am
by Silvio.Falconi
karinha wrote:
Silvio.Falconi wrote:ok but this on win 10, i tried on win 7 and not run ok
https://i.imgur.com/3RrQB3X.png

Image

Right, on Windows Seven Professional run ok
on Windows Seven Home Premium 64 bit with Server Pack 1 not run
is there someone have the same windows I have to try the test ?