Quantcast
Channel: Adobe Community : Popular Discussions - ColdFusion
Viewing all articles
Browse latest Browse all 14291

How to access a JSON structure key that starts with a number

$
0
0

I've got an odd issue here.  I'm accessing someone else's JSON structure so I am not able to change it.  The JSON structure keys are named with both characters and numbers like this:

 

 

 

0198456
product_id198456
1Rashaan Houston feat Tony Loreto
artistRashaan Houston feat Tony Loreto

 

 

So, in other words, there's a key named "0" and a key named "product_id", both of which contain the same key.  Why they did that, I don't know, but they did.

 

In one of the instances, the data stored under the text-named key isn't the same as the numerical-named key.  I tried to output the data in the numerical key like this (it's an array of structures):

 

#strStompyJSON.data[1].0#

 

but I get the following error:

 

--

Invalid CFML construct found on line 41 at column 31.

ColdFusion was looking at the following text:

 

.0

--

 

When I do the following, it works fine:

 

#strStompyJSON.data[1].product_id#

 

 

So it looks as though CF doesn't like accessing a variable that starts with a number.  I am able to loop through the structure using a collection loop, and it outputs all of the keys (including the numerical ones) and their values using the following code:

 

<cfloop collection="#strStompyJSON.data[1]#" item="key">
#key#: #strStompyJSON.data[1][key]#<br />
</cfloop>

 

 

However, that doesn't allow me a way to access specific keys named with a number.  Is there a way that I can specifically access a key that is named with a number?

 

thanks!

Mike


Viewing all articles
Browse latest Browse all 14291

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>