James A Mohler
James A Mohler

Reputation: 11120

String to Array with evaluate() fails

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

Answers (3)

Peter Boughton
Peter Boughton

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

Dan Bracuk
Dan Bracuk

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

duncan
duncan

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

Related Questions