Search in sources :

Example 11 with JSONArray

use of weibo4j.org.json.JSONArray in project twitter-2-weibo by rjyo.

the class Tag method constructTagWapper.

public static TagWapper constructTagWapper(Response res) {
    try {
        JSONArray tags = res.asJSONArray();
        List<Tag> tagList = new ArrayList<Tag>();
        for (int i = 0; i < tags.getJSONObject(0).getJSONArray("tags").length(); i++) {
            tagList.add(new Tag(tags.getJSONObject(0).getJSONArray("tags").getJSONObject(i)));
        }
        String id = tags.getJSONObject(0).getString("id");
        return new TagWapper(tagList, id);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (WeiboException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 12 with JSONArray

use of weibo4j.org.json.JSONArray in project twitter-2-weibo by rjyo.

the class Trends method constructTrendsList.

/* package */
public static List<Trends> constructTrendsList(Response res) throws WeiboException {
    JSONObject json = res.asJSONObject();
    List<Trends> trends;
    try {
        Date asOf = parseDate(json.getString("as_of"));
        JSONObject trendsJson = json.getJSONObject("trends");
        trends = new ArrayList<Trends>(trendsJson.length());
        Iterator ite = trendsJson.keys();
        while (ite.hasNext()) {
            String key = (String) ite.next();
            JSONArray array = trendsJson.getJSONArray(key);
            Trend[] trendsArray = jsonArrayToTrendArray(array);
            if (key.length() == 19) {
                // current trends
                trends.add(new Trends(res, asOf, parseDate(key, "yyyy-MM-dd HH:mm:ss"), trendsArray));
            } else if (key.length() == 16) {
                // daily trends
                trends.add(new Trends(res, asOf, parseDate(key, "yyyy-MM-dd HH:mm"), trendsArray));
            } else if (key.length() == 10) {
                // weekly trends
                trends.add(new Trends(res, asOf, parseDate(key, "yyyy-MM-dd"), trendsArray));
            }
        }
        Collections.sort(trends);
        return trends;
    } catch (JSONException jsone) {
        throw new WeiboException(jsone.getMessage() + ":" + res.asString(), jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) Iterator(java.util.Iterator) JSONArray(weibo4j.org.json.JSONArray) JSONException(weibo4j.org.json.JSONException) Date(java.util.Date)

Example 13 with JSONArray

use of weibo4j.org.json.JSONArray in project twitter-2-weibo by rjyo.

the class Trends method constructTrends.

/* package */
static Trends constructTrends(Response res) throws WeiboException {
    JSONObject json = res.asJSONObject();
    try {
        Date asOf = parseDate(json.getString("as_of"));
        JSONArray array = json.getJSONArray("trends");
        Trend[] trendsArray = jsonArrayToTrendArray(array);
        return new Trends(res, asOf, asOf, trendsArray);
    } catch (JSONException jsone) {
        throw new WeiboException(jsone.getMessage() + ":" + res.asString(), jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONArray(weibo4j.org.json.JSONArray) JSONException(weibo4j.org.json.JSONException) Date(java.util.Date)

Example 14 with JSONArray

use of weibo4j.org.json.JSONArray in project twitter-2-weibo by rjyo.

the class User method constructWapperUsers.

/**
	 * 
	 * @param res
	 * @return
	 * @throws WeiboException
	 */
public static UserWapper constructWapperUsers(Response res) throws WeiboException {
    //asJSONArray();
    JSONObject jsonUsers = res.asJSONObject();
    try {
        JSONArray user = jsonUsers.getJSONArray("users");
        int size = user.length();
        List<User> users = new ArrayList<User>(size);
        for (int i = 0; i < size; i++) {
            users.add(new User(user.getJSONObject(i)));
        }
        long previousCursor = jsonUsers.getLong("previous_curosr");
        long nextCursor = jsonUsers.getLong("next_cursor");
        long totalNumber = jsonUsers.getLong("total_number");
        String hasvisible = jsonUsers.getString("hasvisible");
        return new UserWapper(users, previousCursor, nextCursor, totalNumber, hasvisible);
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 15 with JSONArray

use of weibo4j.org.json.JSONArray in project twitter-2-weibo by rjyo.

the class User method constructResult.

/**
	 * @param res 
	 * @return 
	 * @throws WeiboException
	 */
static List<User> constructResult(Response res) throws WeiboException {
    JSONArray list = res.asJSONArray();
    try {
        int size = list.length();
        List<User> users = new ArrayList<User>(size);
        for (int i = 0; i < size; i++) {
            users.add(new User(list.getJSONObject(i)));
        }
        return users;
    } catch (JSONException e) {
    }
    return null;
}
Also used : JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Aggregations

JSONArray (weibo4j.org.json.JSONArray)16 JSONException (weibo4j.org.json.JSONException)16 ArrayList (java.util.ArrayList)13 JSONObject (weibo4j.org.json.JSONObject)5 Date (java.util.Date)2 Iterator (java.util.Iterator)1