1. 程式人生 > >Java JSONObject後首字母變小寫的問題

Java JSONObject後首字母變小寫的問題

public static String formatJson(String orgJson) {
  JSONObject jo = new JSONObject();
  try {
   JSONObject jsonObject = new JSONObject(orgJson);
   Iterator iterator = jsonObject.keys();

   while (iterator.hasNext()) {
    String key = (String) iterator.next();
    char chars[] = key.toCharArray();
    if (key.length() > 1 && Character.isLowerCase(key.charAt(0))) {
     chars[0] = Character.toUpperCase(chars[0]);
    }

    Boolean bl = jsonObject.isNull(key);
    if (bl) {
     jo.put(new String(chars), null);
    } else {
     Object object = jsonObject.get(key);
     try {
      if (object instanceof Number) {
       // Log.i("MainActivity-----------------",
       // "result:1");
       jo.put(new String(chars),
         ((Number) object).intValue());
      } else {
       // Log.i("MainActivity-----------------",
       // "result:2");
       jo.put(new String(chars), jsonObject.getString(key));
      }
     } catch (Exception e) {
      jo.put(new String(chars), jsonObject.getString(key));
     }
    }
   }
  } catch (Exception e) {

   e.printStackTrace();
  }
  return jo.toString();
 }