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

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

Postby fafi » Tue May 31, 2016 11:22 am

Helloo Mr. Rao
Please test !

Code: Select all  Expand view

#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
fafi
 
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

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

Postby Antonio Linares » Tue May 31, 2016 8:05 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41405
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

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

Postby nageswaragunupudi » Wed Jun 01, 2016 9: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: Select all  Expand view
#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: 10308
Joined: Sun Nov 19, 2006 5:22 am
Location: India

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

Postby nageswaragunupudi » Thu Jun 02, 2016 1: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: Select all  Expand view
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  Expand view
#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
nageswaragunupudi
 
Posts: 10308
Joined: Sun Nov 19, 2006 5:22 am
Location: India

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

Postby fafi » Fri Jun 03, 2016 3:59 am

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

Best Regars
Fafi
User avatar
fafi
 
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

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

Postby TOTOVIOTTI » Mon Dec 05, 2016 7: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.edu.ar
User avatar
TOTOVIOTTI
 
Posts: 387
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 ]

Postby Silvio.Falconi » 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: Select all  Expand view
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  Expand view
#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 November 2023 - January 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
User avatar
Silvio.Falconi
 
Posts: 6849
Joined: Thu Oct 18, 2012 7:17 pm

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

Postby karinha » Mon Jan 18, 2021 1:42 pm

https://i.imgur.com/WTKQrTg.png

Image

Code: Select all  Expand view

#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
User avatar
karinha
 
Posts: 7343
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

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

Postby Silvio.Falconi » Mon Jan 18, 2021 2: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 November 2023 - January 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
User avatar
Silvio.Falconi
 
Posts: 6849
Joined: Thu Oct 18, 2012 7:17 pm

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

Postby karinha » Mon Jan 18, 2021 2: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

Image
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7343
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

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

Postby Silvio.Falconi » Thu Jan 21, 2021 8: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

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 ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 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
User avatar
Silvio.Falconi
 
Posts: 6849
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Enrico Maria Giordano, Horizon and 31 guests