Esempio n. 1
0
 public JSONObject getJSONObject(String key) throws JSONException {
   JSONValue v = get(key);
   if (v == null) throw new JSONException(JSONErrorCode.KeyIsNotFound, key);
   JSONObject o = v.getJSONObject();
   if (o == null) throw new JSONException(JSONErrorCode.NotAJSONObject, v.toString());
   return o;
 }
Esempio n. 2
0
  public String getString(String key) throws JSONException {
    JSONValue v = get(key);
    if (v == null) throw new JSONException(JSONErrorCode.KeyIsNotFound, key);

    JSONString s = v.getJSONString();
    if (s == null) throw new JSONException(JSONErrorCode.NotAJSONString, v.toString());
    return s.getValue();
  }
Esempio n. 3
0
  public JSONArray getJSONArray(String key) {
    JSONValue v = get(key);
    if (v == null) return null;

    JSONArray a = v.getJSONArray();
    if (a == null) return null;
    return a;
  }
Esempio n. 4
0
 public static JSONArray parseJSONArray(String jsonText) {
   if (jsonText != null && 0 < jsonText.length()) {
     JSONValue jsonValue = JSONParser.parse(jsonText);
     if (jsonValue != null) {
       return jsonValue.isArray();
     }
   }
   return null;
 }
Esempio n. 5
0
  public int getInt(String key) throws JSONException {
    JSONValue v = get(key);
    if (v == null) throw new JSONException(JSONErrorCode.KeyIsNotFound, key);

    JSONNumber n = v.getJSONNumber();
    if (n == null) throw new JSONException(JSONErrorCode.NotAJSONNumber, v.toString());

    return n.getIntValue();
  }
Esempio n. 6
0
 //
 // Utility methods, also useful when parsing third-party JSON
 //
 public static JSONObject parseJSONObject(String jsonText) {
   if (jsonText != null && 0 < jsonText.length()) {
     // TODO: use GWT json utils class to do safe parsing
     JSONValue jsonValue = JSONParser.parse(jsonText);
     if (jsonValue != null) {
       return jsonValue.isObject();
     }
   }
   return null;
 }
Esempio n. 7
0
 public static Float jsonValueToFloat(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return (float) jsonNumber.doubleValue();
     }
   }
   return null;
 }
Esempio n. 8
0
 public static Double jsonValueToDouble(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return jsonNumber.doubleValue();
     }
   }
   return null;
 }
Esempio n. 9
0
 public static Boolean jsonValueToBoolean(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONBoolean jsonBoolean = jsonValue.isBoolean();
     if (jsonBoolean != null) {
       return jsonBoolean.booleanValue();
     }
   }
   return null;
 }
Esempio n. 10
0
 public static Integer jsonValueToInteger(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return (int) jsonNumber.doubleValue();
     }
   }
   return null;
 }
Esempio n. 11
0
 public static Long jsonValueToLong(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return (long) jsonNumber.doubleValue();
     }
   }
   return null;
 }
Esempio n. 12
0
 public static String jsonValueToString(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONString jsonString = jsonValue.isString();
     if (jsonString == null) {
       return null;
     } else {
       return jsonString.stringValue();
     }
   }
   return null;
 }
Esempio n. 13
0
  /**
   * Convert a list to JSON text. The result is a JSON array. If this list is also a JSONAware,
   * JSONAware specific behaviours will be omitted at this top level.
   *
   * @see org.json.simple.JSONValue#toJSONString(Object)
   * @param list
   * @return JSON text, or "null" if list is null.
   */
  public static String toJSONString(List list) {
    if (list == null) return "null";

    boolean first = true;
    StringBuffer sb = new StringBuffer();
    Iterator iter = list.iterator();

    sb.append('[');
    while (iter.hasNext()) {
      if (first) first = false;
      else sb.append(',');

      Object value = iter.next();
      if (value == null) {
        sb.append("null");
        continue;
      }
      sb.append(JSONValue.toJSONString(value));
    }
    sb.append(']');
    return sb.toString();
  }
Esempio n. 14
0
  /**
   * Encode a list into JSON text and write it to out. If this list is also a JSONStreamAware or a
   * JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
   *
   * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
   * @param list
   * @param out
   */
  public static void writeJSONString(List list, Writer out) throws IOException {
    if (list == null) {
      out.write("null");
      return;
    }

    boolean first = true;
    Iterator iter = list.iterator();

    out.write('[');
    while (iter.hasNext()) {
      if (first) first = false;
      else out.write(',');

      Object value = iter.next();
      if (value == null) {
        out.write("null");
        continue;
      }

      JSONValue.writeJSONString(value, out);
    }
    out.write(']');
  }
Esempio n. 15
0
 public static JSONArray jsonValueToArray(JSONValue jsonValue) {
   if (jsonValue != null) {
     return jsonValue.isArray();
   }
   return null;
 }
Esempio n. 16
0
 public static JSONObject jsonValueToObject(JSONValue jsonValue) {
   if (jsonValue != null) {
     return jsonValue.isObject();
   }
   return null;
 }
Esempio n. 17
0
 // This method assumes that the JSON value being passed is a JSON
 // non-primitive:
 // either an object or an array.
 // This helps to make this implementation as efficient as possible.
 protected String jsonNonPrimitiveToPrettyString(JSONValue jsonValue, int indentLevel) {
   StringBuffer buffer = new StringBuffer();
   // If the value in question is an object
   JSONObject jsonValueObject = jsonValue.isObject();
   if (jsonValueObject != null) {
     boolean firstKey = true;
     for (String key : jsonValueObject.keySet()) {
       if (firstKey) {
         firstKey = false;
       } else {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       buffer.append(key).append(" : ");
       JSONValue jsonObjectValue = jsonValueObject.get(key);
       if (jsonObjectValue != null) {
         if (jsonObjectValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("}");
         } else if (jsonObjectValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonObjectValue.toString());
         }
       }
     }
   } else if (jsonValue.isArray() != null) {
     // If the value in question is an array
     JSONArray jsonValueArray = jsonValue.isArray();
     for (int i = 0; i < jsonValueArray.size(); ++i) {
       if (0 < i) {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       JSONValue jsonArrayValue = jsonValueArray.get(i);
       if (jsonArrayValue != null) {
         if (jsonArrayValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("}");
         } else if (jsonArrayValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonArrayValue.toString());
         }
       }
     }
   }
   return buffer.toString();
 }
Esempio n. 18
0
  public void parseJSON(String singleLine) {
    JSONObject json = (JSONObject) JSONValue.parse(singleLine);
    JSONArray jry = (JSONArray) json.get("statuses");
    if (jry == null) {
      return;
    }

    for (Object jry1 : jry) {
      JSONObject obj, pobj;
      obj = (JSONObject) jry1;
      pobj = (JSONObject) obj.get("user");
      JsonTweet jt = new JsonTweet(obj.get("id_str").toString(), pobj.get("id_str").toString());
      jt.setText(obj.get("text").toString());
      jt.setRetweetCount(obj.get("retweet_count").toString());
      jt.setFavoriteCount(obj.get("favorite_count").toString());
      if (obj.get("contributors") != null) {
        jt.setContributors(obj.get("contributors").toString());
      }
      jt.setLang(obj.get("lang").toString());
      if (obj.get("geo") != null) {
        jt.setGeo(obj.get("geo").toString());
      }
      jt.setSource(obj.get("source").toString());
      jt.setCreatedAt(obj.get("created_at").toString());
      if (obj.get("place") != null) {
        jt.setPlace(obj.get("place").toString());
      }
      jt.setRetweeted(obj.get("retweeted").toString());
      jt.setTruncated(obj.get("truncated").toString());
      jt.setFavorited(obj.get("favorited").toString());
      if (obj.get("coordinates") != null) {
        jt.setCoordinates(obj.get("coordinates").toString());
      }
      JsonTweetUser jtu = jt.getUser();
      jtu.setLocation(pobj.get("location").toString());
      jtu.setDefaultProfile(pobj.get("default_profile").toString());
      jtu.setBackgroundTile(pobj.get("profile_background_tile").toString());
      jtu.setStatusesCount(pobj.get("statuses_count").toString());
      jtu.setLang(pobj.get("lang").toString());
      jtu.setFollowing(pobj.get("following").toString());
      jtu.setProtected(pobj.get("protected").toString());
      jtu.setFavoriteCount(pobj.get("favourites_count").toString());
      jtu.setDescription(pobj.get("description").toString());
      jtu.setVerified(pobj.get("verified").toString());
      jtu.setContributorsEnabled(pobj.get("contributors_enabled").toString());
      jtu.setname(pobj.get("name").toString());
      jtu.setCreatedAt(pobj.get("created_at").toString());
      jtu.setTranslationEnabled(pobj.get("is_translation_enabled").toString());
      jtu.setDefaultProfileImg(pobj.get("default_profile_image").toString());
      jtu.setFollowerCount(pobj.get("followers_count").toString());
      if (pobj.get("has_extended_profile") != null) {
        jtu.setExtendedProfile(pobj.get("has_extended_profile").toString());
      }
      jtu.setProfileImg(pobj.get("profile_image_url_https").toString());
      jtu.setGeoEnabled(pobj.get("geo_enabled").toString());
      jtu.setProfileBackgroundImg(pobj.get("profile_background_image_url_https").toString());
      if (pobj.get("url") != null) {
        jtu.setUrl(pobj.get("url").toString());
      }
      if (pobj.get("utc_offset") != null) {
        jtu.setUtcOffset(pobj.get("utc_offset").toString());
      }
      if (pobj.get("time_zone") != null) {
        jtu.setTimeZone(pobj.get("time_zone").toString());
      }
      if (pobj.get("notifications") != null) {
        jtu.setNotifications(pobj.get("notifications").toString());
      }
      jtu.setFiendCount(pobj.get("friends_count").toString());
      jtu.setScreenName(pobj.get("screen_name").toString());
      jtu.setListedCount(pobj.get("listed_count").toString());
      jtu.setIsTranslator(pobj.get("is_translator").toString());
      tweets.add(jt);
    }
  }