public static String getJSONString(Object data) {

String str = "";

Object obj = null;

if(data instanceof JSON) {

if(data instanceof JSONArray) {

JSONArray arr = (JSONArray) data;

for(int i = 0; i < arr.size(); i++) {

if(arr.get(i) instanceof JSON) {

String s = getJSONString(arr.get(i));

arr.set(i, JSON.parse(s, Feature.OrderedField));

}

}

str = arr.toJSONString();

}else if(data instanceof JSONObject) {

JSONObject objj = (JSONObject) data;

List lstk = JSON.parseArray(JSON.toJSONString(objj.keySet()), String.class);

lstk.sort(new Comparator() {

@Override

public int compare(String o1, String o2) {

// TODO Auto-generated method stub

return o1.compareTo(o2);

}

});

JSONObject objd = new JSONObject(true);

for(String k : lstk) {

if(objj.get(k) instanceof JSON) {

String s = getJSONString(objj.get(k));

objd.put(k, JSON.parse(s, Feature.OrderedField));

}else {

objd.put(k, objj.get(k));

}

}

str = objd.toJSONString();

}

}else {

if(data != null) {

str = data.toString();

}

}

return str;

}