I am trying to send a list to my extension, which then needs to be converted to a string, and then any double quotes need to be replaced with single quotes. The extension constructs an html file as a string, which is why I do not need/want to use the list directly. This is what I have so far (just the code for this):
public static void main(String[] args) {
System.out.println(Arrays.toString(parse(new Object[]{"\"hello\"", 1,Arrays.toString(parse(new Object[]{"\"hello\"", 1}))})));
}
public static Object[] parse(Object[] arr){
Object[] rArr = new Object[arr.length];
for (int i=0;i<arr.length;i++){
Object o = arr[i];
if (o instanceof String){
rArr[i] = o.toString().replaceAll("\"","'");
}else if(o instanceof Object[]){
Object[] innerArr = (Object[]) o;
rArr[i] = parse(innerArr);
}else {
rArr[i] = o;
}
}
return rArr;
}
Output: ['hello', 1, ['hello', 1]]
@oseamiya I appreciate your efforts but there is a small flaw in your code and that's what if user inputs an Array of Object Arrays (not String Array).
Looks like I will need to iterate over the yailist
I found the java code for the join block in the sources, this simply uses .toString() to convert a list to a string in the blocks, so was hoping it might have as straight forward in java code....
I just realized that "string" and ""string"" are not same.
So it is not possible to replace " with ' in a string object (because we can't call it a string then).
If you want to replace latter:
public YailList RMethod(YailList yailList){
Object[] o = parse(yailList.toArray());
return YailList.makeList(o);
}
public Object[] parse(Object[] arr){
Object[] rArr = new Object[arr.length];
for (int i=0;i<arr.length;i++){
Object o = arr[i];
if (o instanceof String){
rArr[i] = o.toString().replace("\"","'");
}else if(o instanceof Object[]){
Object[] innerArr = (Object[]) o;
rArr[i] = parse(innerArr);
}else {
rArr[i] = o;
}
}
return rArr;
}
See attached a working aia project for a pie chart, and the java code used to build the extension. Note: this uses the blocks procedure to convert the AI2 list to the correct string.