FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to Decode Json From web service [ to Mr. Rao ]
Posts: 169
Joined: Mon Feb 25, 2008 02:42 AM
How to Decode Json From web service [ to Mr. Rao ]
Posted: Tue May 31, 2016 11:22 AM
Helloo Mr. Rao
Please test !

Code (fw): Select all Collapse
#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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Tue May 31, 2016 08:05 PM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Wed Jun 01, 2016 09:23 AM
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 (fw): Select all Collapse
#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)


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


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



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Thu Jun 02, 2016 01:24 AM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
#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
Regards



G. N. Rao.

Hyderabad, India
Posts: 169
Joined: Mon Feb 25, 2008 02:42 AM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Fri Jun 03, 2016 03:59 AM

Terimakasih Mr. Rao ( Indonesian )
Thank you so much

Best Regars
Fafi

:D:D

Posts: 470
Joined: Fri Feb 05, 2010 11:30 AM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Mon Dec 05, 2016 07:41 PM

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

Univ@c I.S.I.
Desarrolladores de Software
http://www.elcolegioencasa.ar
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Mon Jan 18, 2021 12:34 PM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
#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
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Mon Jan 18, 2021 01:42 PM
https://i.imgur.com/WTKQrTg.png



Code (fw): Select all Collapse
#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
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Mon Jan 18, 2021 02:21 PM

ok but this on win 10, i tried on win 7 and not run ok

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Mon Jan 18, 2021 02:47 PM
Silvio.Falconi wrote:ok but this on win 10, i tried on win 7 and not run ok


https://i.imgur.com/3RrQB3X.png

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: How to Decode Json From web service [ to Mr. Rao ]
Posted: Thu Jan 21, 2021 08:35 AM
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




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 ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com

Continue the discussion