Lists from Json

Lists from Json

Postby byron.hopp » Fri Dec 06, 2019 3:55 pm

How do you read an item from a list in a hash created from a json response.
Look for the "[" in the example response below:

If I used hb_JsonDecode( cJsonResponse,@o )

Then how do I get the second set of variables from the hash?

o["transactionResponse"]["userFields"]["name"]

How do I get to the second set of "name/value" in this hash.

{
"transactionResponse": {
"responseCode": "1",
"authCode": "C1E3I6",
"avsResultCode": "Y",
"cvvResultCode": "S",
"cavvResultCode": "9",
"transId": "2149186775",
"refTransID": "",
"transHash": "C85B15CED28462974F1114DB07A16C39",
"accountNumber": "XXXX0015",
"accountType": "Mastercard",
"messages": [
{
"code": "1",
"description": "This transaction has been approved."
}
],
"userFields": [
{
"name": "MerchantDefinedFieldName1",
"value": "MerchantDefinedFieldValue1"
},
{
"name": "favorite_color",
"value": "blue"
}
]
},
"refId": "123456",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}

Thanks,

Byron ...
Thanks,
Byron Hopp
Matrix Computer Services
byron.hopp
 
Posts: 386
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Lists from Json

Postby cnavarro » Fri Dec 06, 2019 6:05 pm

Byron, try with this: ( "userFields": [ ( is a array of hash ) )
Look in xbrowse, in "userFields" show {=>}{=>} ( two hashs )
Code: Select all  Expand view  RUN

#include "Fivewin.ch"

//----------------------------------------------------------------------------//
// If I used hb_JsonDecode( cJsonResponse,@o )
// Then how do I get the second set of variables from the hash?
// o["transactionResponse"]["userFields"]["name"]
//----------------------------------------------------------------------------//

Function Main()

   Local cText
   Local hHash

   TEXT INTO cText
   {
    "transactionResponse": {
    "responseCode": "1",
    "authCode": "C1E3I6",
    "avsResultCode": "Y",
    "cvvResultCode": "S",
    "cavvResultCode": "9",
    "transId": "2149186775",
    "refTransID": "",
    "transHash": "C85B15CED28462974F1114DB07A16C39",
    "accountNumber": "XXXX0015",
    "accountType": "Mastercard",
    "messages": [
                  {
                  "code": "1",
                  "description": "This transaction has been approved."
                  }
    ],
    "userFields": [
                  {
                  "name": "MerchantDefinedFieldName1",
                  "value": "MerchantDefinedFieldValue1"
                  },
                  {
                  "name": "favorite_color",
                  "value": "blue"
                  }
      ]
   },
   "refId": "123456",
   "messages": {
      "resultCode": "Ok",
      "message": [
                  {
                  "code": "I00001",
                  "text": "Successful."
                  }
       ]
      }
   }
   ENDTEXT

   hb_JsonDecode( cText, @hHash )
   XBrowse( hHash )
   ? hHash["transactionResponse"]["userFields"][ 1 ]["name"]
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: 6549
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Lists from Json

Postby byron.hopp » Fri Dec 06, 2019 10:16 pm

Very cool, thank you for the reply, I will try it.
Makes sense.

Byron ...
Thanks,
Byron Hopp
Matrix Computer Services
byron.hopp
 
Posts: 386
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 42 guests