Java Json String Create toJSON(String string)

Here you can find the source of toJSON(String string)

Description

to JSON

License

Apache License

Declaration

public static String toJSON(String string) 

Method Source Code

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

public class Main {
    static final String HEXES = "0123456789abcdef";

    public static String toJSON(String string) {
        if (string != null) {
            string = string.replace("\"", "\\\"");
        }/*from  ww w .  j  a v  a  2s. c o  m*/
        return string;

    }

    public static String replace(String string, int startIndex, int endIndex, String newContent) {
        StringBuilder builder = new StringBuilder();
        builder.append(string.substring(0, startIndex));
        builder.append(newContent);
        builder.append(string.substring(endIndex, string.length()));
        String replaced = builder.toString();
        return replaced;
    }

    /**
     * Try and provide some nice formatting for this object
     */
    private static String toString(Object fieldValue) {
        String toString;

        if (fieldValue instanceof byte[]) {
            toString = toHex((byte[]) fieldValue);
        } else {
            toString = fieldValue.toString();
        }

        return toString;
    }

    public static String toHex(byte[] raw) {
        if (raw == null) {
            return null;
        }
        int index = 0;
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));

            if (index < raw.length - 1) {
                hex.append(" ");
            }

            index++;
        }
        return hex.toString();
    }
}

Related

  1. toJson(String jsonp)
  2. toJson(String org)
  3. toJson(String s)
  4. toJson(String str)
  5. toJson(String str)
  6. toJsonArrays(String... strings)
  7. toJsonField(String name, String value)
  8. toJsonName(String name, int prefixLen)
  9. toJsonUrl(String pUrl)