Lists from Json

Post Reply
byron.hopp
Posts: 254
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA
Contact:

Lists from Json

Post by byron.hopp »

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

Re: Lists from Json

Post by cnavarro »

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

Code: Select all

#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
 
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.
byron.hopp
Posts: 254
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA
Contact:

Re: Lists from Json

Post by byron.hopp »

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

Byron ...
Thanks,
Byron Hopp
Matrix Computer Services
Post Reply