Java Json Create convertToJson(Object myObject)

Here you can find the source of convertToJson(Object myObject)

Description

convert To Json

License

LGPL

Declaration

public static String convertToJson(Object myObject) 

Method Source Code

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

import java.util.Map;

public class Main {
    public static String convertToJson(Object myObject) {
        if (myObject instanceof Map<?, ?>) {
            final Map<?, ?> map = (Map<?, ?>) myObject;
            final StringBuilder sb = new StringBuilder("{");
            boolean first = true;
            for (final Map.Entry<?, ?> entry : map.entrySet()) {
                if (first) {
                    first = false;//  ww  w .  j a  va2 s  .  c  o m
                } else {
                    sb.append(",");
                }
                sb.append("\n\t'").append(entry.getKey()).append("':'").append(entry.getValue()).append("'");
            }
            if (!first) {
                sb.append("\n");
            }
            sb.append("}");
            System.out.println(sb.toString());
            return sb.toString();
        } else {
            System.out.println(myObject);
        }
        return "";
    }
}

Related

  1. build(Map map)
  2. buildContentResponse(String userId, String message)
  3. buildJsonArray(String[][] twoDimensionArray)
  4. convertToJson(Object obj)
  5. createArrayBuilder()
  6. createArrayBuilder()
  7. createJsonFrom(JsonObject user, String... ignoreKeys)