Java Json Parse jsonString(String s)

Here you can find the source of jsonString(String s)

Description

json String

License

Open Source License

Declaration

public static String jsonString(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class Main {
    static char[] hex = "0123456789ABCDEF".toCharArray();

    public static String jsonString(String s) {
        if (s == null)
            return null;
        StringBuffer b = new StringBuffer();
        b.append("\"");
        CharacterIterator it = new StringCharacterIterator(s);
        for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
            if (c == '"')
                b.append("\\\"");
            else if (c == '\\')
                b.append("\\\\");
            else if (c == '/')
                b.append("\\/");
            else if (c == '\b')
                b.append("\\b");
            else if (c == '\f')
                b.append("\\f");
            else if (c == '\n')
                b.append("\\n");
            else if (c == '\r')
                b.append("\\r");
            else if (c == '\t')
                b.append("\\t");
            else if (Character.isISOControl(c)) {
                int n = c;
                for (int i = 0; i < 4; ++i) {
                    int digit = (n & 0xf000) >> 12;
                    b.append(hex[digit]);
                    n <<= 4;/*from w w  w  . jav a  2  s  . c  o m*/
                }
            } else {
                b.append(c);
            }
        }
        b.append("\"");
        return b.toString();
    }
}

Related

  1. getMapFromGson(String json)
  2. json2Obj(String json, Class clazz)
  3. parse(JSONObject jsonobj)
  4. parse(String json, Class tClass)
  5. parseDateFromJsonArray(final JsonArray array)
  6. parseJson(String jsonText, String[] params)