Example #1
1
    public void generateObjects(String arrayName, Object... parent) {
      if (mRec == null) return;

      Object rawObj;
      try {
        rawObj = mParser.parse(mJson);
        if (rawObj == null) return;

        JSONObject obj = (JSONObject) rawObj;
        JSONArray arr = (JSONArray) obj.get(arrayName);
        int cnt = 0;
        if (arr != null) {
          for (Object jb : arr.toArray()) // each object
          {
            JSONObject jo = (JSONObject) jb;
            mCls.getConstructors()[0].setAccessible(true);

            Object w;
            if (parent != null && parent.length != 0) // Inner class
            w = mCls.getConstructors()[0].newInstance(parent[0]);
            else {
              w = mCls.getConstructors()[0].newInstance((Object) null);
            }
            for (Field f : mCls.getDeclaredFields()) // each field
            {
              try {
                Object fieldVal = jo.get(f.getName());
                if (fieldVal == null) continue;
                if (f.getType() == Integer.class) f.set(w, Integer.parseInt(fieldVal.toString()));
                else if (f.getType() == Date.class)
                  f.set(w, new Date(Long.parseLong(fieldVal.toString())));
                else if (f.getType() == String.class) f.set(w, fieldVal.toString());
              } catch (IllegalArgumentException e) {
                e.printStackTrace();
              } catch (IllegalAccessException e) {
                e.printStackTrace();
              }
            }
            mRec.receiveObject(w);
          }
        }
      } catch (ParseException e1) {
        e1.printStackTrace();
      } catch (IllegalArgumentException e1) {
        e1.printStackTrace();
      } catch (SecurityException e1) {
        e1.printStackTrace();
      } catch (InstantiationException e1) {
        e1.printStackTrace();
      } catch (IllegalAccessException e1) {
        e1.printStackTrace();
      } catch (InvocationTargetException e1) {
        e1.printStackTrace();
      }
    }
Example #2
0
  @RequiresLogin
  public void put() throws IOException {
    String path = getPath();
    String json = getBody();
    Object obj = JSONValue.parse(json);

    if (obj instanceof JSONArray) {
      JSONArray array = (JSONArray) obj;
      addEdits(path, (JSONObject[]) array.toArray(new JSONObject[0]));
    } else {
      addEdits(path, (JSONObject) obj);
    }
  }