Пример #1
0
  public static void main(String[] args) throws JSONException {
    User wuzbin = new User();
    wuzbin.setAge(20);
    wuzbin.setName("wuzbin");

    User cg = new User();
    cg.setAge(10);
    cg.setName("cg");

    List<User> users = new ArrayList<User>();
    users.add(wuzbin);
    users.add(cg);

    JsonBuffer jb = new JsonBuffer();
    jb.appendList(
        "users",
        users,
        new JsonEncoder<User>() {
          @Override
          public void encode(User user, JsonBuffer jsonBuffer) throws JSONException {
            jsonBuffer.appendInteger("age", user.getAge());
            jsonBuffer.appendString("name", user.getName());
          }
        });
    System.out.println(jb.toString());
  }
Пример #2
0
 public <T> void appendList(String key, T[] list, JsonEncoder<T> encoder) throws JSONException {
   JSONArray array = new JSONArray();
   object.put(key, array);
   for (T t : list) {
     JsonBuffer jsonBuffer = new JsonBuffer();
     encoder.encode(t, jsonBuffer);
     array.put(jsonBuffer.getJsonObject());
   }
 }
Пример #3
0
 public <T> void appendList(
     String key, Iterable<T> list, JsonEncoder<T> encoder, Criteria<T> criteria)
     throws JSONException {
   JSONArray array = new JSONArray();
   object.put(key, array);
   for (T t : list) {
     if (criteria != null) {
       if (criteria.isMeet(t)) {
         JsonBuffer jsonBuffer = new JsonBuffer();
         encoder.encode(t, jsonBuffer);
         array.put(jsonBuffer.getJsonObject());
       }
     } else {
       JsonBuffer jsonBuffer = new JsonBuffer();
       encoder.encode(t, jsonBuffer);
       array.put(jsonBuffer.getJsonObject());
     }
   }
 }
Пример #4
0
 public <T> void appendObject(String key, T t, JsonEncoder<T> encoder) throws JSONException {
   JsonBuffer jsonBuffer = new JsonBuffer();
   encoder.encode(t, jsonBuffer);
   object.put(key, jsonBuffer.getJsonObject());
 }