Beispiel #1
0
  private static String parseText(String tweet) throws JSONException {
    JSONObject json = new JSONObject(tweet);

    JSONObject jsonText = json.getJSONObject("text");
    String text = jsonText.getString("text");
    return text;
  }
 TimeZoneJSONImpl(JSONObject jSONObject) throws TwitterException {
   try {
     UTC_OFFSET = getInt("utc_offset", jSONObject);
     NAME = jSONObject.getString("name");
     TZINFO_NAME = jSONObject.getString("tzinfo_name");
   } catch (JSONException jsone) {
     throw new TwitterException(jsone);
   }
 }
 private AccountSettingsJSONImpl(HttpResponse res, JSONObject json) throws TwitterException {
   super(res);
   try {
     JSONObject sleepTime = json.getJSONObject("sleep_time");
     SLEEP_TIME_ENABLED = getBoolean("enabled", sleepTime);
     SLEEP_START_TIME = sleepTime.getString("start_time");
     SLEEP_END_TIME = sleepTime.getString("end_time");
     if (json.isNull("trend_location")) {
       TREND_LOCATION = new Location[0];
     } else {
       JSONArray locations = json.getJSONArray("trend_location");
       TREND_LOCATION = new Location[locations.length()];
       for (int i = 0; i < locations.length(); i++) {
         TREND_LOCATION[i] = new LocationJSONImpl(locations.getJSONObject(i));
       }
     }
     GEO_ENABLED = getBoolean("geo_enabled", json);
     LANGUAGE = json.getString("language");
     ALWAYS_USE_HTTPS = getBoolean("always_use_https", json);
     DISCOVERABLE_BY_EMAIL = getBoolean("discoverable_by_email", json);
     TIMEZONE = new TimeZoneJSONImpl(json.getJSONObject("time_zone"));
     SCREEN_NAME = json.getString("screen_name");
   } catch (JSONException e) {
     throw new TwitterException(e);
   }
 }
 private void decode(String str) {
   if (str != null && str.startsWith("{")) {
     try {
       JSONObject json = new JSONObject(str);
       if (!json.isNull("error")) {
         this.errorMessage = json.getString("error");
       }
       if (!json.isNull("request")) {
         this.requestPath = json.getString("request");
       }
     } catch (JSONException ignore) {
     }
   }
 }
 /*package*/ RelationshipJSONImpl(HttpResponse res, JSONObject json) throws TwitterException {
   super(res);
   try {
     JSONObject relationship = json.getJSONObject("relationship");
     JSONObject sourceJson = relationship.getJSONObject("source");
     JSONObject targetJson = relationship.getJSONObject("target");
     sourceUserId = getLong("id", sourceJson);
     targetUserId = getLong("id", targetJson);
     sourceUserScreenName = getUnescapedString("screen_name", sourceJson);
     targetUserScreenName = getUnescapedString("screen_name", targetJson);
     sourceBlockingTarget = getBoolean("blocking", sourceJson);
     sourceFollowingTarget = getBoolean("following", sourceJson);
     sourceFollowedByTarget = getBoolean("followed_by", sourceJson);
     sourceNotificationsEnabled = getBoolean("notifications_enabled", sourceJson);
   } catch (JSONException jsone) {
     throw new TwitterException(jsone.getMessage() + ":" + json.toString(), jsone);
   }
 }
 private void init(JSONObject jsonobject) {
   try {
     JSONArray jsonarray = jsonobject.getJSONArray("indices");
     setStart(jsonarray.getInt(0));
     setEnd(jsonarray.getInt(1));
     if (!jsonobject.isNull("name")) {
       name = jsonobject.getString("name");
     }
     if (!jsonobject.isNull("screen_name")) {
       screenName = jsonobject.getString("screen_name");
     }
     id = z_T4JInternalParseUtil.getLong("id", jsonobject);
     return;
   }
   // Misplaced declaration of an exception variable
   catch (JSONObject jsonobject) {
     throw new TwitterException(jsonobject);
   }
 }
  private void init(JSONObject json) throws TwitterException {
    id = getLong("id", json);
    text = getUnescapedString("text", json);
    source = getUnescapedString("source", json);
    createdAt = getDate("created_at", json);
    isTruncated = getBoolean("truncated", json);
    inReplyToStatusId = getLong("in_reply_to_status_id", json);
    inReplyToUserId = getLong("in_reply_to_user_id", json);
    isFavorited = getBoolean("favorited", json);
    inReplyToScreenName = getUnescapedString("in_reply_to_screen_name", json);
    retweetCount = getLong("retweet_count", json);
    try {
      if (!json.isNull("user")) {
        user = new UserJSONImpl(json.getJSONObject("user"));
      }
    } catch (JSONException jsone) {
      throw new TwitterException(jsone);
    }
    geoLocation = z_T4JInternalJSONImplFactory.createGeoLocation(json);
    if (!json.isNull("place")) {
      try {
        place = new PlaceJSONImpl(json.getJSONObject("place"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse place:" + json);
      }
    }

    if (!json.isNull("retweeted_status")) {
      try {
        retweetedStatus = new StatusJSONImpl(json.getJSONObject("retweeted_status"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse retweeted_status:" + json);
      }
    }
    if (!json.isNull("contributors")) {
      try {
        JSONArray contributorsArray = json.getJSONArray("contributors");
        contributorsIDs = new long[contributorsArray.length()];
        for (int i = 0; i < contributorsArray.length(); i++) {
          contributorsIDs[i] = Long.parseLong(contributorsArray.getString(i));
        }
      } catch (NumberFormatException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse contributors:" + json);
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse contributors:" + json);
      }
    } else {
      contributors = null;
    }
    if (!json.isNull("entities")) {
      try {
        JSONObject entities = json.getJSONObject("entities");
        int len;
        if (!entities.isNull("user_mentions")) {
          JSONArray userMentionsArray = entities.getJSONArray("user_mentions");
          len = userMentionsArray.length();
          userMentionEntities = new UserMentionEntity[len];
          for (int i = 0; i < len; i++) {
            userMentionEntities[i] =
                new UserMentionEntityJSONImpl(userMentionsArray.getJSONObject(i));
          }
        }
        if (!entities.isNull("urls")) {
          JSONArray urlsArray = entities.getJSONArray("urls");
          len = urlsArray.length();
          urlEntities = new URLEntity[len];
          for (int i = 0; i < len; i++) {
            urlEntities[i] = new URLEntityJSONImpl(urlsArray.getJSONObject(i));
          }
        }

        if (!entities.isNull("hashtags")) {
          JSONArray hashtagsArray = entities.getJSONArray("hashtags");
          len = hashtagsArray.length();
          hashtagEntities = new HashtagEntity[len];
          for (int i = 0; i < len; i++) {
            hashtagEntities[i] = new HashtagEntityJSONImpl(hashtagsArray.getJSONObject(i));
          }
        }

        if (!entities.isNull("media")) {
          JSONArray mediaArray = entities.getJSONArray("media");
          len = mediaArray.length();
          mediaEntities = new MediaEntity[len];
          for (int i = 0; i < len; i++) {
            mediaEntities[i] = new MediaEntityJSONImpl(mediaArray.getJSONObject(i));
          }
        }
      } catch (JSONException jsone) {
        throw new TwitterException(jsone);
      }
    }
    if (!json.isNull("annotations")) {
      try {
        JSONArray annotationsArray = json.getJSONArray("annotations");
        annotations = new Annotations(annotationsArray);
      } catch (JSONException ignore) {
      }
    }
    if (!json.isNull("current_user_retweet")) {
      try {
        myRetweetedStatus = new StatusJSONImpl(json.getJSONObject("current_user_retweet"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse current_user_retweet:" + json);
      }
    }
  }
  public MediaEntityJSONImpl(JSONObject json) throws TwitterException {
    try {
      JSONArray indicesArray = json.getJSONArray("indices");
      this.start = indicesArray.getInt(0);
      this.end = indicesArray.getInt(1);
      this.id = getLong("id", json);

      try {
        this.url = new URL(json.getString("url"));
      } catch (MalformedURLException ignore) {
      }

      if (!json.isNull("expanded_url")) {
        try {
          this.expandedURL = new URL(json.getString("expanded_url"));
        } catch (MalformedURLException ignore) {
        }
      }
      if (!json.isNull("media_url")) {
        try {
          this.mediaURL = new URL(json.getString("media_url"));
        } catch (MalformedURLException ignore) {
        }
      }
      if (!json.isNull("media_url_https")) {
        try {
          this.mediaURLHttps = new URL(json.getString("media_url_https"));
        } catch (MalformedURLException ignore) {
        }
      }
      if (!json.isNull("display_url")) {
        this.displayURL = json.getString("display_url");
      }
      JSONObject sizes = json.getJSONObject("sizes");
      this.sizes = new HashMap<Integer, MediaEntity.Size>(4);
      this.sizes.put(MediaEntity.Size.LARGE, new Size(sizes.getJSONObject("large")));
      this.sizes.put(MediaEntity.Size.MEDIUM, new Size(sizes.getJSONObject("medium")));
      this.sizes.put(MediaEntity.Size.SMALL, new Size(sizes.getJSONObject("small")));
      this.sizes.put(MediaEntity.Size.THUMB, new Size(sizes.getJSONObject("thumb")));
      if (!json.isNull("type")) {
        this.type = json.getString("type");
      }
    } catch (JSONException jsone) {
      throw new TwitterException(jsone);
    }
  }
 Size(JSONObject json) throws JSONException {
   width = json.getInt("w");
   height = json.getInt("h");
   resize =
       "fit".equals(json.getString("resize")) ? MediaEntity.Size.FIT : MediaEntity.Size.CROP;
 }
 public void testGetLong() throws Exception {
   JSONObject json = new JSONObject("{\"value\":\"13857270119014401\"}");
   assertEquals(13857270119014401l, json.getLong("value"));
 }
  private static JSONObject validateJSONObjectSchema(JSONObject json, String[] knownNames)
      throws JSONException {
    boolean debug = false;
    Map<String, String[]> schemaMap = new HashMap<String, String[]>();
    List<String> names = new ArrayList<String>();
    if (debug) {
      System.out.println("validating:" + json);
    }
    for (int i = 0; i < knownNames.length; i++) {
      if (debug) {
        System.out.println("knownName[" + i + "]:" + knownNames[i]);
      }
      String knownName = knownNames[i];
      int index;
      if (-1 != (index = knownName.indexOf("/"))) {
        String parent = knownName.substring(0, index);
        String child = knownName.substring(index + 1);
        String[] array = schemaMap.get(parent);
        if (null == array) {
          schemaMap.put(parent, new String[] {child});
        } else {
          String[] newArray = new String[array.length + 1];
          System.arraycopy(array, 0, newArray, 0, array.length);
          newArray[newArray.length - 1] = child;
          schemaMap.put(parent, newArray);
        }
        names.add(parent);
      } else {
        names.add(knownName);
      }
    }

    Iterator ite = json.keys();
    while (ite.hasNext()) {
      String name = (String) ite.next();
      boolean found = false;
      if (debug) {
        System.out.println("name:" + name);
      }
      for (String elementName : names) {
        if (debug) {
          System.out.println("elementname:" + elementName);
        }
        Object obj = json.get(name);
        if (obj instanceof JSONObject || obj instanceof JSONArray) {
          String[] children = schemaMap.get(name);
          if (null == children) {
            Assert.fail(
                elementName + ":" + name + " is not supposed to have any child but has:" + obj);
          } else if (!children[0].equals("*")) {
            validateJSONSchema(obj, children);
          }
        }
        if (elementName.equals(name)) {
          found = true;
          break;
        }
      }
      if (!found) {
        Assert.fail("unknown element:[" + name + "] in " + json);
      }
    }
    return json;
  }