Link to home
Start Free TrialLog in
Avatar of hefterr
hefterrFlag for United States of America

asked on

Coldfusion Complex Structure Reference

Hello,
I am getting a complex JSON file and I convert it to a complex structure using deserializeJSON().

The JSON represents game results for a game site.  As part of the embedded JSON the players are referred to as "1" and "-1" for the player that goes first and second respectively.  The code include 3 game examples and is runnable as is.

With my reference code I can get to the data for player "1" but I throw an error for player "-1" which I commented out in the code.  The error is "A CFML variable name cannot end with a "." character"

If I edit the file and change the "-1" to "2", then my reference works fine.  I can do this with an editor (textpad).  I just was wondering if there is a syntax I can use to leave the file as is and avoid a manual step.  FYI I am using Coldfusion 8.



Thanks in advance,
hefterr

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<!--Test JSON parsing -->

<cfsavecontent variable="apidata">
[{
		"id" : "1caf00170c5fdc9b31435d97949e324a",
		"status" : "played",
		"created" : 1337076473000,
		"players" : {
			"1" : {
				"uid" : 52,
				"picture" : "https://www.jocly.com/sites/default/files/styles/thumbnail/public/pictures/picture-52-1328533101.png",
				"name" : "jcfrog",
				"country" : "FRANCE",
				"countryCode" : "FR"
			},
			"-1" : {
				"uid" : 1,
				"picture" : "https://www.jocly.com/sites/default/files/styles/thumbnail/public/pictures/picture-1-1320702482.png",
				"name" : "admin",
				"country" : "FRANCE",
				"countryCode" : "FR"
			}
		},
		"endReason" : "regular",
		"winner" : -1
	},
	{
		"id" : "6c959eb636a80905e0c1284a6d77e76a",
		"status" : "played",
		"created" : 1337113665000,
		"players" : {
			"1" : {
				"uid" : 858,
				"picture" : "https://www.jocly.com/sites/all/themes/joclook/images/default-avatar.png",
				"name" : "florian",
				"country" : "FRANCE",
				"countryCode" : "FR"
			},
			"-1" : {
				"uid" : 462,
				"picture" : "https://www.jocly.com/sites/default/files/styles/thumbnail/public/pictures/picture-462-1333741762.jpg",
				"name" : "YO",
				"country" : "FRANCE",
				"countryCode" : "FR"
			}
		},
		"endReason" : "regular",
		"winner" : 1
	},
	{
		"id" : "91671cdf9fcb55a8afa27955b6f75629",
		"status" : "played",
		"created" : 1337113882000,
		"players" : {
			"1" : {
				"uid" : 462,
				"picture" : "https://www.jocly.com/sites/default/files/styles/thumbnail/public/pictures/picture-462-1333741762.jpg",
				"name" : "YO",
				"country" : "FRANCE",
				"countryCode" : "FR"
			},
			"-1" : {
				"uid" : 858,
				"picture" : "https://www.jocly.com/sites/all/themes/joclook/images/default-avatar.png",
				"name" : "florian",
				"country" : "FRANCE",
				"countryCode" : "FR"
			}
		},
		"endReason" : "regular",
		"winner" : -1
	}
 ]   
</cfsavecontent>


<cfset record = deserializeJSON(apiData) />

<cfdump var="#record#">
 <cfloop from="1"  to="#arraylen(record)#" index="i">
    <cfoutput>#record[i].players.1.name# </cfoutput>
    <!--- <cfoutput>#record[i].players.-1.name# </cfoutput> --->
</cfloop>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hefterr

ASKER

Hi _agx_,
I had started to realize (before reading this) that the "1" and "-1" were actually variable names and were not valid in CF.  I was planing on using a REPLACE() function to change the "1" to "P1" and the "-1" to "P2".  Not sure why it worked when the name was 1,2,3 etc but not negative?

But obviously, your solution is best!!!!!!
Avatar of hefterr

ASKER

Thanks again for you help!!!!
Not sure why it worked when the name was 1,2,3 etc but not negative?

Truthfully I wouldn't have expected positive numbers to work either, but I guess CF is smart enough to figure it out. The dashes are a deal breaker though. I don't think those are ever valid when using dot notation.  The compiler probably interprets them as something other than a variable name. Then doesn't know what to do from there and falls over with an error.