Search in sources :

Example 36 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class StringTest_00 method test_string.

public void test_string() throws Exception {
    char[] chars = new char[1024];
    Arrays.fill(chars, '0');
    StringBuilder buf = new StringBuilder();
    buf.append("[\"");
    for (int i = 0; i < 16; ++i) {
        buf.append("\\\\");
        buf.append(new String(chars));
    }
    buf.append("\"]");
    String text = buf.toString();
    JSONArray array = (JSONArray) JSON.parse(text);
    Assert.assertEquals(1, array.size());
    String item = (String) array.get(0);
    Assert.assertEquals(16 * 1024 + 16, item.length());
    for (int i = 0; i < 16; ++i) {
        Assert.assertTrue(item.charAt(i * 1025) == '\\');
        for (int j = 0; j < 1024; ++j) {
            Assert.assertTrue(item.charAt(i * 1025 + j + 1) == '0');
        }
    }
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 37 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class JSONPath_set_test2 method test_jsonpath.

public void test_jsonpath() throws Exception {
    JSONObject rootObject = JSON.parseObject("{\"array\":[{},{},{},{}]}");
    JSONPath.set(rootObject, "$.array[0:].key", "123");
    JSONArray array = rootObject.getJSONArray("array");
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals("123", array.getJSONObject(i).get("key"));
    }
    System.out.println(rootObject);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 38 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class RefTest5 method test_parse.

public void test_parse() throws Exception {
    Object[] array2 = JSON.parseObject("[[{\"$ref\":\"..\"}]]", Object[].class);
    JSONArray item = (JSONArray) array2[0];
    Assert.assertSame(item, item.get(0));
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 39 with JSONArray

use of com.alibaba.fastjson.JSONArray in project AisenWeiBo by wangdan.

the class SinaSDK method searchsSuggest.

/**
	 * 使用H5页面的接口拉取数据
	 *
	 * @param q
	 * @param cookies
	 * @return
	 * @throws TaskException
	 */
public String[] searchsSuggest(String q, String cookies) throws TaskException {
    // http://m.weibo.cn/searchs/suggest?count=10&q=ann
    Setting action = newSetting("searchsSuggest", "searchs/suggest", "获取搜索建议");
    action.getExtras().put(BASE_URL, newSettingExtra(BASE_URL, "http://m.weibo.cn/", ""));
    Params params = new Params();
    params.addParameter("q", q);
    params.addParameter("count", "5");
    HttpConfig config = configHttpConfig();
    config.cookie = cookies;
    config.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    config.addHeader("Referer", "http://m.weibo.cn/searchs");
    try {
        // ["ana",["anastasia","T-ANA小芹","anastasia 修容","广安门医院官方微博","anastasia 高光"]]
        String response = doPost(config, action, null, params, null, String.class);
        response = AisenUtils.convertUnicode(response);
        Logger.d("SinaSDK", response);
        if (response.toLowerCase().indexOf("<html>") != -1) {
            throw new TaskException("cookieinvalid", "网页版登录失效了");
        }
        // {"request":"/search/suggestions/all.php","error_code":"21405","error":"Operation timed out after 300 milliseconds with 0 bytes received url:http://i.api.weibo.com/users/show_batch.json"}
        if (response.indexOf("error_code") != -1 && response.indexOf("error") != -1) {
            JSONObject jsonRespone = JSONObject.parseObject(response);
            throw new TaskException(jsonRespone.getString("error_code"), jsonRespone.getString("error"));
        } else // {"ok":-100,"msg":"请先登录","url":"https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2Fsearchs%2Fsuggest"}
        if (response.indexOf("ok") != -1 && response.indexOf("msg") != -1) {
            JSONObject jsonRespone = JSONObject.parseObject(response);
            throw new TaskException(jsonRespone.getString("ok"), jsonRespone.getString("msg"));
        }
        JSONArray jsonArray = JSON.parseArray(response);
        JSONArray resultArray = jsonArray.getJSONArray(1);
        String[] result = new String[resultArray.size()];
        for (int i = 0; i < resultArray.size(); i++) {
            result[i] = resultArray.getString(i);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof TaskException) {
            throw e;
        }
    }
    return new String[0];
}
Also used : TaskException(org.aisen.android.network.task.TaskException) JSONObject(com.alibaba.fastjson.JSONObject) Setting(org.aisen.android.common.setting.Setting) JSONArray(com.alibaba.fastjson.JSONArray) Params(org.aisen.android.network.http.Params) HttpConfig(org.aisen.android.network.http.HttpConfig) ParseException(java.text.ParseException) TaskException(org.aisen.android.network.task.TaskException)

Example 40 with JSONArray

use of com.alibaba.fastjson.JSONArray in project AisenWeiBo by wangdan.

the class SinaSDK method searchsResultStatuss.

/**
	 * H5接口搜索微博
	 *
	 * @param q
	 * @param page
	 * @param cookies
	 * @return
	 * @throws TaskException
	 */
public ArrayList<StatusContent> searchsResultStatuss(String q, int page, String cookies) throws TaskException {
    ArrayList<StatusContent> resultUsers = new ArrayList<>();
    Setting action = newSetting("searchsResultUsers", "page/pageJson", "获取用户");
    action.getExtras().put(BASE_URL, newSettingExtra(BASE_URL, "http://m.weibo.cn/", ""));
    Params params = new Params();
    params.addParameter("containerid", "100103type%3D2%26q%3D" + q + "&page=" + page);
    try {
        String response = doGet(getHttpConfig(), action, params, String.class);
        JSONObject responseJSON = JSONObject.parseObject(response);
        int ok = responseJSON.getInteger("ok");
        if (ok == 1) {
            JSONArray cardsArray = responseJSON.getJSONArray("cards");
            for (int i = 0; i < cardsArray.size(); i++) {
                JSONObject cardGroupsObject = cardsArray.getJSONObject(i);
                JSONArray cardGroupArray = cardGroupsObject.getJSONArray("card_group");
                for (int j = 0; j < cardGroupArray.size(); j++) {
                    JSONObject cardGroup = cardGroupArray.getJSONObject(j);
                    JSONObject mblogObject = cardGroup.getJSONObject("mblog");
                    StatusContent content = JSON.parseObject(mblogObject.toJSONString(), StatusContent.class);
                    // 图片
                    if (mblogObject.containsKey("pics")) {
                        JSONArray picsArray = mblogObject.getJSONArray("pics");
                        if (picsArray != null && picsArray.size() > 0) {
                            PicUrls picUrls = new PicUrls();
                            picUrls.setThumbnail_pic(picsArray.getJSONObject(0).getString("url"));
                            content.setPic_urls(new PicUrls[] { picUrls });
                        }
                    }
                    // 把Html5文本转换一下
                    content.setText(Html.fromHtml(content.getText()).toString());
                    if (content.getRetweeted_status() != null) {
                        content.getRetweeted_status().setText(Html.fromHtml(content.getRetweeted_status().getText()).toString());
                    }
                    // 把时间转换一下
                    try {
                        Calendar calendar = Calendar.getInstance();
                        int year = calendar.get(Calendar.YEAR);
                        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
                        calendar.setTimeInMillis(format.parse(content.getCreated_at()).getTime());
                        calendar.set(Calendar.YEAR, year);
                        content.setCreated_at(calendar.getTimeInMillis() + "");
                    } catch (ParseException e) {
                        try {
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            content.setCreated_at(format.parse(content.getCreated_at()).getTime() + "");
                        } catch (ParseException ewe) {
                        }
                    }
                    resultUsers.add(content);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof TaskException) {
            throw (TaskException) e;
        }
    }
    return resultUsers;
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) Setting(org.aisen.android.common.setting.Setting) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) Params(org.aisen.android.network.http.Params) ParseException(java.text.ParseException) TaskException(org.aisen.android.network.task.TaskException) JSONObject(com.alibaba.fastjson.JSONObject) PicUrls(org.aisen.weibo.sina.sinasdk.bean.PicUrls) TaskException(org.aisen.android.network.task.TaskException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)95 JSONObject (com.alibaba.fastjson.JSONObject)39 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 StringReader (java.io.StringReader)6 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)5 Reader (java.io.Reader)5 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)3 Map (java.util.Map)3 Setting (org.aisen.android.common.setting.Setting)3 JSONReader (com.alibaba.fastjson.JSONReader)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2