Java List Transform transformToJsonArray(List objectsWithJsonToString)

Here you can find the source of transformToJsonArray(List objectsWithJsonToString)

Description

Object.toString() has to return json

License

Apache License

Declaration

public static String transformToJsonArray(List<?> objectsWithJsonToString) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {
    /**/*www.  j  a va2 s  .  c  o m*/
     * Object.toString() has to return json
     */
    public static String transformToJsonArray(List<?> objectsWithJsonToString) {

        StringBuilder json = new StringBuilder("[");
        for (Object object : objectsWithJsonToString) {
            json.append(object.toString()).append(",");
        }

        if (objectsWithJsonToString.size() > 0 && json.charAt(json.length() - 1) == ',') {
            json = new StringBuilder(json.substring(0, json.length() - 1));
        }

        json.append("]");
        return json.toString();
    }
}

Related

  1. transformIntoAList(String... elements)
  2. transformNumberedPropertiesToList(Properties props)
  3. transformStringListToString(List fileContentAsList)
  4. transformStringToListOfStrings(String line)
  5. transformToDoublePrimitiveArray( List values)