1
votes

I have a custom rest control written in Java. Everything works fine for string fields, but not for multi-value string fields. My code returns

"[Value1, Value2, Value3...]". Note there are no commas around the values. On the web page the output looks like this:

enter image description here

If I can get commas around the values, the front-end framework can easily parse it.

I have tried to parse the value from the field and to get it formatted correctly, but cannot seem to get it right.

The first set of code works for a string. The second is an attempt to work for a multi-value field.

Any help would be greatly appreciated.

writer.startProperty("chgTitle");
      writer.outStringLiteral(String.valueOf(columnValues.get(0)));
writer.endProperty();

writer.startProperty("plpAffected");    
                Object tmpObj = columnValues.get(5);
                if (tmpObj instanceof Vector) {
                       String[] copyArr = new String[((Vector) tmpObj).size()];
                       ((Vector) tmpObj).copyInto(copyArr);
                       writer.outStringLiteral(String.valueOf(copyArr));
                } else {
                }
writer.endProperty();
1

1 Answers

2
votes

I would recommend a slightly different approach. I like to force my JSON structures to use an array anywhere a multiple value situation is possible. It also looks like you're using Philippe Riand's specialized JSON writer, so I'll assume that.

Here's about how I would attack it:

writer.startProperty("chgTitle");
      writer.outStringLiteral(String.valueOf(columnValues.get(0)));
writer.endProperty();

writer.startProperty("plpAffected");    
    Vector<?> tmpVec = Util.getValueAsVector(columnValues.get(5));
    writer.startArray();
    for ( Object ob : tmpVec ) {
        writer.startArrayItem();
            // assuming String contents
            writer.outStringLiteral(String.valueOf(ob));
        writer.endArrayItem();
    }
    writer.endArray();
writer.endProperty();

To wrap the returning columnValues, I'm using a Java equivalent of my SSJS getValueAsVector helper function. It checks for Vector or ArrayList, which I happen to use almost exclusively; if it's not, it shoves it into a new Vector. Here's what that method looks like,

public static Vector<?> getValueAsVector(Object obj) {
  if(obj instanceof java.util.Vector){
    return (Vector<?>) obj;
  }else if( obj instanceof ArrayList ) {
    List<?> o = (List<?>) obj;
    Vector<Object> tmpVec = new Vector<Object>();
    for(int i=0;i<o.size();i++){
      tmpVec.add(o.get(i));
    }
    return tmpVec;
  }else {
    Vector<Object> tmpVec = new Vector<Object>();
    tmpVec.add(obj);
    return tmpVec;
  }
}