0

I have a velocity variable, like this: $cur_record.getFieldValue("SelectRoles", $locale) that is supposed to be an array. If I print its value, (just by putting $cur_record.getFieldValue("SelectRoles", $locale) in the code) i get: ["Accountant","Cashier"]

now, i want to iterate those 2 values, Accountant and Cashier, but it seems to be a String, not an Array, how can i convert that to an array so I can iterate it?..

I have tried to iterate it, but does not work, like this:

#foreach($bla_role in $cur_record.getFieldValue("SelectRoles", $locale))
    $bla_role
#end

Also tried to get the value, as if it were an array, does not work either:

$cur_record.getFieldValue("SelectRoles", $locale).get(0)

I've tried setting it to another variable, like this:

#set($roleval = $cur_record.getFieldValue("SelectRoles", $locale))
$roleval.get(0)

but it does not work, but if i set a string, as the value is printed (the value hard coded), it does work!, like this:

#set($roleval = ["Accountant","Cashier"])
$roleval.get(0)

I dont know if I have to escape something, or I am missing something, can some one help me?

thank you!

juanmeza
  • 163
  • 1
  • 12
  • This might be helpful: http://stackoverflow.com/questions/8751387/how-to-access-elements-of-array-after-using-string-split-in-velocity – Parkash Kumar Oct 08 '15 at 05:28
  • Thanks Parkash Kumar, ive seen that post already, didnt work for me :( – juanmeza Oct 08 '15 at 06:01
  • What happen when you say "does not work"? Do you have some error message? What you get printing `$cur_record.getFieldValue("SelectRoles", $locale).class.getName()`? – Marco Mercuri Oct 08 '15 at 07:48
  • I mean it does not iterate, or doesnt print nothing, i did the class.getName and it is a string (confirmed now), thats why it does not iterate or get the values... how can i convert that to an array? – juanmeza Oct 08 '15 at 15:08
  • To investigate the problem, you can display the class name of an object with `$my_object.class.name`. Maybe you can give us some more informations with this trick. – Claude Brisson Oct 09 '15 at 15:10

1 Answers1

1

You're trying to parse a String array that was previously serialized to String.

The following snippet uses substring, split and replace String methods to parse it.

#set($roleval = '["Accountant","Cashier"]')
#set($rolevalLengthMinusOne = $roleval.length() - 1)
#set($roles = $roleval.substring(1, $rolevalLengthMinusOne).split(","))

#foreach($role in $roles)
    <h1>$role.replace('"',"")</h1>
#end

At first I tried to use #evaluate to parse it, but I ended up with these String methods.

Tomas Pinos
  • 2,812
  • 14
  • 22
  • thank you @Tomáš Piňos, i tried your answer and it works, but it only works if i initialize the string as you mention... but my value comes from another variable, how can i initialize the **roleval** variable?, i tried this: **#set($roleval = $cur_record.getFieldValue("SelectRoles", $locale))** but did not work, also this **#set($roleval = '$cur_record.getFieldValue("SelectRoles", $locale)')** but did not work either... can you help me? – juanmeza Oct 22 '15 at 02:59
  • I need to know the value of `$roleval.class.name`. – Tomas Pinos Oct 22 '15 at 07:19
  • I had something wrong in my code, it does work!! thank you!! – juanmeza Oct 23 '15 at 03:32
  • You're welcome. If you like my answer, please accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in. http://stackoverflow.com/help/someone-answers – Tomas Pinos Oct 23 '15 at 07:26