Page 1 of 1

Hash when decoding JSON [ Mr. Rao ]

PostPosted: Wed Jul 19, 2017 9:52 pm
by TimStone
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

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Thu Jul 20, 2017 4:42 am
by anserkk
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

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Thu Jul 20, 2017 4:11 pm
by cnavarro
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
.../...
 

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Thu Jul 20, 2017 4:24 pm
by TimStone
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


 

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Fri Jul 21, 2017 8:32 am
by dagiayunus
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

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Fri Jul 21, 2017 4:06 pm
by TimStone
Unfortunately that doesn't resolve the problem. There is still an error with &

Tim

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Fri Jul 21, 2017 5:30 pm
by Enrico Maria Giordano
Tim,

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

EMG

Re: Hash when decoding JSON [ Mr. Rao ]

PostPosted: Fri Jul 21, 2017 6:42 pm
by TimStone
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