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

Post Reply
User avatar
fafi
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

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

Post 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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

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

Post 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
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

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

Post 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
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
fafi
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

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

Post by fafi »

:D :D
Terimakasih Mr. Rao ( Indonesian )
Thank you so much

Best Regars
Fafi
User avatar
TOTOVIOTTI
Posts: 334
Joined: Fri Feb 05, 2010 11:30 am
Location: San Francisco - Córdoba - Argentina

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

Post 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
Univ@c I.S.I.
Desarrolladores de Software
http://www.elcolegioencasa.edu.ar
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

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

Post 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
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

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

Post 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
 
João Santos - São Paulo - Brasil
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

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

Post by Silvio.Falconi »

ok but this on win 10, i tried on win 7 and not run ok
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

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

Post 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
João Santos - São Paulo - Brasil
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

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

Post 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 ?
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Post Reply