Hash when decoding JSON [ Mr. Rao ]

Hash when decoding JSON [ Mr. Rao ]

Postby TimStone » Wed Jul 19, 2017 9:52 pm

In a previous thread, you shared the following function:

Code: Select all  Expand view


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

 


Unfortunately the line hHash := &cStr creates an error with &.
I am building this whith FWH 17.06 plus Harbour and MSVC++ 2017

Do you have an alternative ?

The ultimate objective is this. I receive a JSON document. It is built as follows:

SECTION 1

SECTION 2

SECTION 3 // This is the data I want

Subsection 3A
DetailSections // There may be several of these
Details

Subsection 3B
Activity // There will be many of these
Details // Each of these may have multiple lines

I will be reading all lines in Section 3, in sequence, and will want to add each line to an array. Ultimately this will display a BROWSER display.

Currently, if I use hb_JSONdecode( file ) and put that in a browser, it shows me the 3 sections. Then there are [=>] marks for each subsection. I click on those and get a new browser. Within those I see the detail sections with the same symbol. I click on the value and it opens another browser to reveal the data.

So ... putting the values into a hash table would be helpful because then I should be able to pick up the values and add them to my displayable browser so I can see everything at once.

Again, this is not to find individual data, but actually a viewer to see all of the data

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: 2903
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Hash when decoding JSON [ Mr. Rao ]

Postby anserkk » Thu Jul 20, 2017 4:42 am

May be you can use something similar as follows, you may have to substitute the names as per your JSON file

hb_jsonDecode(cResponse,@hJson)
xValue:= hJson["Section3"]["Subsection3A"]["DetailSections"]["Details"]

Regards

Anser
User avatar
anserkk
 
Posts: 1329
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Hash when decoding JSON [ Mr. Rao ]

Postby cnavarro » Thu Jul 20, 2017 4:11 pm

I think the best solution is provided by Mr Anser, but this code can solve their error

Code: Select all  Expand view

.../...
  cStr  := '{"response"=>' + cStr + '}'
  hHash := &cStr
.../...
 
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: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Hash when decoding JSON [ Mr. Rao ]

Postby TimStone » Thu Jul 20, 2017 4:24 pm

I think the problem is the use of & .

Here is the code section, and suggested function, which I have tried both ways. Since Mr. Rao offered this solution in another thread, I was hopeful he would clarify. What is important to know is that I must read, and save, every value of the return data. jsondecode( ) does place the data in a hash file, but I have not worked with hash files over all these years, so I'm treading into new territory.

So here is the cut from the code that is giving the problem:

Code: Select all  Expand view

    oHttp:Send(cJson)
  cRet := ""
  cRtext := oHttp:responseText
 
  hb_jsondecode( cRtext, @cRet )
 
  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, '":', '"=>' )           // Original code suggested by Mr. Rao
   cStr  := '{"response"=>' + cStr + '}'            // Alternate code suggested to solve the problem

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

return nil


 
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: 2903
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Hash when decoding JSON [ Mr. Rao ]

Postby dagiayunus » Fri Jul 21, 2017 8:32 am

Dear Mr. Tim

I had similar problem for getting JSON data for my program using TELEGRAM.

Code: Select all  Expand view

      cStr  := (StrTran( cStr, '":', '"=>' ))
      cStr  := (StrTran( cStr, '""', '"' ))  //add this line
      hHash := &cStr
 


Regards
Dagia Yunus.
Rajkot, India

FWH 17.04
dagiayunus
 
Posts: 69
Joined: Wed Nov 19, 2014 1:04 pm

Re: Hash when decoding JSON [ Mr. Rao ]

Postby TimStone » Fri Jul 21, 2017 4:06 pm

Unfortunately that doesn't resolve the problem. There is still an error with &

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: 2903
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Hash when decoding JSON [ Mr. Rao ]

Postby Enrico Maria Giordano » Fri Jul 21, 2017 5:30 pm

Tim,

it would be much easier if you could build a reduced PRG and data showing the problem.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8307
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Hash when decoding JSON [ Mr. Rao ]

Postby TimStone » Fri Jul 21, 2017 6:42 pm

Enrico,

Sometimes that isn't possible due to an NDA that does not allow me to post some links / data.

Let me put it this way. Mr. Rao suggested this code as an alternative to hb_jsondecode( )
The value I receive from the server is a properly formatted json response.

If I use the hb_jsondecode( ) function to return a hash file cRet, and then look at it with xbrowser, everything is properly handled.

In studying Mr. Rao's solution with the exact same json response, not encoded as a hash, it has an error with the & symbol. It doesn't matter if I use xHarbour ( .com ) or Harbour.

My issue is I want to translate ALL lines of the json response, and write them to an array, not just find one specific value. I was looking at Mr. Rao's solution as a possible way toward doing this since it was given in response to another person who also wanted to put a json response into an array rather than a hash file.

So ... the error is a runtime error, and it specifically states the error is with &.

So I can stick with the hash ... and just figure out how to work with that to get what I want which is a multi level parsing of the data.

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: 2903
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: Google [Bot] and 19 guests