serialize Map<String,?> To String - Android java.lang

Android examples for java.lang:String Join

Description

serialize Map<String,?> To String

Demo Code

import java.util.List;
import java.util.Map;

public class Main{

    public static String serializeToString(Map<String, ?> data) {
        StringBuilder result = new StringBuilder();
        int index = 0;
        for (Map.Entry<String, ?> e : data.entrySet()) {
            String key = e.getKey();
            String value = toString(e.getValue());
            value = value.replace("\n", "\\n"); // we use new lines as separators
            result.append(key);/*from w ww. ja va 2  s . c o m*/
            result.append(':');
            result.append(value);

            if (++index < data.size()) {
                result.append("\n");
            }
        }

        return result.toString();
    }

}

Related Tutorials