Hash when decoding JSON [ Mr. Rao ]

Post Reply
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Hash when decoding JSON [ Mr. Rao ]

Post by TimStone »

In a previous thread, you shared the following function:

Code: Select all


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
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Hash when decoding JSON [ Mr. Rao ]

Post 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
User avatar
cnavarro
Posts: 5792
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Hash when decoding JSON [ Mr. Rao ]

Post by cnavarro »

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

Code: Select all

.../...
  cStr  := '{"response"=>' + cStr + '}'
  hHash := &cStr
.../...
 
C. Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
Si alguien te dice que algo no se puede hacer, recuerda que esta hablando de sus limitaciones, no de las tuyas.
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Re: Hash when decoding JSON [ Mr. Rao ]

Post 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

    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
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
dagiayunus
Posts: 69
Joined: Wed Nov 19, 2014 1:04 pm
Contact:

Re: Hash when decoding JSON [ Mr. Rao ]

Post by dagiayunus »

Dear Mr. Tim

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

Code: Select all

      cStr  := (StrTran( cStr, '":', '"=>' ))
      cStr  := (StrTran( cStr, '""', '"' ))  //add this line
      hHash := &cStr
 
Regards
Dagia Yunus.
Rajkot, India

FWH 17.04
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Re: Hash when decoding JSON [ Mr. Rao ]

Post by TimStone »

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

Tim
Tim Stone
http://www.MasterLinkSoftware.com
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Hash when decoding JSON [ Mr. Rao ]

Post by Enrico Maria Giordano »

Tim,

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

EMG
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Re: Hash when decoding JSON [ Mr. Rao ]

Post 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
Tim Stone
http://www.MasterLinkSoftware.com
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
Post Reply