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');
}
}
}
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);
}
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));
}
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];
}
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;
}
Aggregations