JSON response

JSON response

Postby cdmmaui » Tue Jan 02, 2018 11:32 pm

Happy New Year Everyone!

I see lots of information about JSON. I was wondering if there was function or sample code that can read JSON response and store to an array?

Thank you!
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON response

Postby cnavarro » Tue Jan 02, 2018 11:46 pm

Try with

Code: Select all  Expand view


Function MyJson( cStr )

   local hHash
   local nLen

   nLen    := hb_JsonDecode( cStr, @hHash )
   if !Empty( nLen )
       XBrowser hHash Setup ( oBrw:AutoFit() )
   endif

Return nil

 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: JSON response

Postby cdmmaui » Wed Jan 03, 2018 12:18 pm

Dear Cristobal,

Thank you! Do you have an example that would load to an array?

Sincerely,
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: JSON response

Postby AntoninoP » Wed Jan 03, 2018 1:03 pm

verily hb_JsonDecode return the type based on text, the code
Code: Select all  Expand view
proc testJSON(cStr)
   LOCAL r := hb_jsonDecode(cStr)
   ? cStr, "-->", r, "("+valtype(r)+")"
 
proc main()
   testJSON('undefined')
   testJSON('true')
   testJSON('54')
   testJSON('"aa"')
   testJSON('{"a":4}')
   testJSON('[1,2,3]')
 

prints:
Code: Select all  Expand view
undefined --> NIL (U)
true --> .T. (L)
54 -->         54 (N)
"aa" --> aa (C)
{"a":4} -->  (H)
[1,2,3] -->  (A)
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: JSON response

Postby TimStone » Wed Jan 03, 2018 6:19 pm

The value of a JSON file is the data encoded in it. Usually, you send a json text, and receive back a response.

Code: Select all  Expand view
 aInfo := { }  
         cJson :='{"vin":"' + cVin + '"}'
         oHttp:Send(cJson)
      cRet := ""
      cRtext := oHttp:responseText
      nLen := hb_jsondecode( cRtext, @cRet )
 


Now cRet contains all of the values. You can add them to an array using:
Code: Select all  Expand view
          AADD( aInfo, "LAST REPORTED SERVICES:")
          FOR EACH o IN cRet["serviceHistory"]["serviceCategories"]
                    SC1 :=  o["serviceName"]
                    SC2 :=  o["dateOfLastService"]
                    nRetItm := 0
                    IF  HHasKey( o, "odometerOfLastService" )
                                    nRetItm := o["odometerOfLastService"]
                        ENDIF          
                    SC3 :=  STR( nRetItm )  //HB_HGETDEF( o, "odometerOfLastService", 0 ),8)
                        aadd( aInfo, ">  " + SC1 + " performed on " + SC2 + "   Odometer: " + SC3 )
          NEXT
 


Who ever supplies the data will give you the identifiers for their JSON fields and the structure. I hope this helps.

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 89 guests