Reputation: 11120
How does one convert a string to an array
<cfscript>
stResult = { strData = "[1,2,3,4,5,6]"
, arInstant = [1,2,3,4,5,6]
};
//stResult.arData = evaluate(stResult.strData); // this does not work
//stResult.arData = evaluate("#stResult.strData#"); // nor does this
writedump(stResult);
</cfscript>
I am trying to get something that looks like arInstant.
Is there a better way than striping the []
, converting to a list, then converting to an array?
Upvotes: 1
Views: 323
Reputation: 112220
Your data is a JSON string so use deserializeJson() to convert it to an array.
Usage:
Result.Data = deserializeJson(Result.Input);
Upvotes: 4
Reputation: 20804
For this specific question, "
Is there a better way than striping the [], converting to a list, then converting to an array?"
Actually, once you strip away the square brackets, it is a list so that's one step less.
Upvotes: 0
Reputation: 31920
evaluate() should generally be avoided (ref: 1, 2, 3). How about trying:
stResult.arData = ListToArray(stResult.strData, "[],");
This is treating the whole string as a list, with possible delimiters of [
and ]
and ,
It should give you an array with 6 elements in it.
Of course, it seems that your stResult.arInstant
already has what you need... what are you trying to do?
Upvotes: 2