-
1. Re: How to access a JSON structure key that starts with a number
msoultan Sep 24, 2010 7:21 PM (in response to msoultan)I made an information page which might be helpful so you can see all the data:
http://cfdev.cota.csulb.edu:8080/stompy/json.cfm
all of the data is live data. Let me know if you have any questions
-
2. Re: How to access a JSON structure key that starts with a number
existdissolve Sep 24, 2010 7:57 PM (in response to msoultan)With ColdFusion structures, you can access keys with dot or bracket notation. As you've discovered, using dot notation doesn't work so well when the key is a number.
So just use bracket notation.
Instead of:
#strStompyJSON.data[1].0#
Try:
#strStompyJSON.data[1][0]#
This looks exactly like the collection loop you posted, and that's because this is really what the collection loop is doing anyway--the only difference is that in this example you've explicitly specified the value of the key instead of having CF iterate through all the keys.
Hope this helps!
Message was edited by: existdissolve
-
3. Re: How to access a JSON structure key that starts with a number
existdissolve Sep 24, 2010 8:28 PM (in response to msoultan)Just out of curiosity, why exactly do you need to reference the numbered keys? From the looks (a quick look, mind you) of the data that is returned, there appears to be a pretty solid correlation between the numbered keys and the labeled keys.
For example, data[x][0] *appears* to line up with data[x]['image'], data[x][1] *appears* to line up with data[x]['artist'], and so on. If these are consistently the same across the data set returned, why try to reference the numeric keys as opposed to the labeled keys?
[Sorry, after posting this I noticed that you laid these out in the link you provided.]
Message was edited by: existdissolve
-
4. Re: How to access a JSON structure key that starts with a number
msoultan Sep 24, 2010 8:56 PM (in response to existdissolve)Well, it was date that I needed to use, but sometimes the two versions don't match up for some reason or another.
Any ideas how to access the numerical values?
thanks!
Mike
-
5. Re: How to access a JSON structure key that starts with a number
existdissolve Sep 24, 2010 10:35 PM (in response to msoultan)Did you try the bracket notation that I suggested in my first response? In my tests with your code, it worked without issue.
Thanks
-
6. Re: How to access a JSON structure key that starts with a number
msoultan Sep 24, 2010 10:48 PM (in response to existdissolve)Oh jeez - I missed that first email of yours. I tried the bracket notation and that worked perfectly!
http://cfdev.cota.csulb.edu:8080/stompy/json.cfm
Thanks!!
Mike
-
7. Re: How to access a JSON structure key that starts with a number
existdissolve Sep 25, 2010 4:31 AM (in response to msoultan)No problem--glad it worked out.
As a follow-up to this, I'd encourage you to keep the bracket notation in mind during future development. It is EXTREMELY useful in a lot of situations.
For example, if you're building a structure on the fly with dynamic keys, bracket notation is extremely helpful:
<cfset authors = structnew()>
<cfloop list="authorlist" index="name">
<cfset authors[name] = getbooklist(name)>
</cfloop>
Also, bracket notation is really nice when creating json from ColdFusion structures. Consider this:
<cfset author = structnew()>
<cfset author.name = "Neil Gaiman">
<cfset author.genre = "Fantasy">
<cfset author.awesome = true>
<cfset authorjson = serializejson(author)>
By default, the serialized json produced will have all UPPER CASE keys. Not a big deal in most cases, but if you're returning the json to something (like a JS library) that is expecting keys in a certain case, it can be a problem. Fortunately, bracket notation allows you to specify exactly what case you want the structure's keys to be in:
<cfset author = structnew()>
<cfset author['name'] = "Neil Gaiman">
<cfset author['Genre'] = "Fantasy">
<cfset author['AWESOME'] = true>
<cfset authorjson = serializejson(author)>
-
8. Re: How to access a JSON structure key that starts with a number
msoultan Sep 25, 2010 8:46 AM (in response to existdissolve)Yeah - I was wondering if there was another type of notation, but I couldn't find it anywhere. Thanks again!!

