下面列出了com.alibaba.fastjson.JSONArray#iterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public JSONObject delDeployServer(String projectId, int id,int page){
DeployServer deployServer = dataService.selectObject(DeployServer.class,"projectId=? and id=?",projectId,id);
if(deployServer == null){
throw new ToClientException(SysConstantDefine.InvalidParam,"server id is not exist!projectId={}, id={}",projectId,id);
}
dataService.delete(deployServer);
JSONObject ret = getDeployServerList(projectId,page,serverPageSize);
JSONArray array = ret.getJSONArray("deployServers");
Iterator<Object> it = array.iterator();
while (it.hasNext()){
JSONObject jsonObject = (JSONObject)it.next();
if(jsonObject.getInteger("id")==id){
it.remove();
break;
}
}
return ret;
}
public static List<TagResult> parseList(String json) {
JSONArray array = JSON.parseArray(json);
List<TagResult> arrayList = new ArrayList<TagResult>(array.size());
Iterator<Object> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject object = (JSONObject) iterator.next();
Set<Entry<String, Object>> entrySet = object.entrySet();
for (Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue().toString();
TagResult tagResult = new TagResult(key, value);
arrayList.add(tagResult);
break;
}
}
return arrayList;
}
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
String sessionId = urlParams.get("sessionId");
Map<String, String> body = new HashMap<String, String>();
UiDevice mDevice = Elements.getGlobal().getmDevice();
JSONObject result = null;
try {
session.parseBody(body);
String postData = body.get("postData");
JSONObject jsonObj = JSON.parseObject(postData);
JSONArray keycodes = (JSONArray)jsonObj.get("value");
for (Iterator iterator = keycodes.iterator(); iterator.hasNext();) {
int keycode = (int) iterator.next();
mDevice.pressKeyCode(keycode);
}
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(result, sessionId).toString());
} catch (Exception e) {
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.UnknownError, sessionId).toString());
}
}
public static <D> List<D> tranJSONArray(JSONArray arr, Class<D> destClass) {
List<D> ret = new ArrayList<>();
Iterator<Object> iterator = arr.iterator();
while (iterator.hasNext()) {
JSONObject next = (JSONObject) iterator.next();
try {
ret.add(destClass.getConstructor(JSONObject.class).newInstance(next));
} catch (Exception e) {
e.printStackTrace();
logger.error("tranJSONArray exception", e);
}
}
return ret;
}
/**
* @param user
* @return
*/
public JSONArray getNavPortal(ID user) {
ConfigEntry config = getLayoutOfNav(user);
if (config == null) {
return NAVS_DEFAULT;
}
// 过滤
JSONArray navs = (JSONArray) config.getJSON("config");
for (Iterator<Object> iter = navs.iterator(); iter.hasNext(); ) {
JSONObject nav = (JSONObject) iter.next();
JSONArray subNavs = nav.getJSONArray("sub");
if (subNavs != null && !subNavs.isEmpty()) {
for (Iterator<Object> subIter = subNavs.iterator(); subIter.hasNext(); ) {
JSONObject subNav = (JSONObject) subIter.next();
if (isFilterNav(subNav, user)) {
subIter.remove();
}
}
// 无子级,移除主菜单
if (subNavs.isEmpty()) {
iter.remove();
}
} else if (isFilterNav(nav, user)) {
iter.remove();
}
}
return navs;
}
/**
* 丰富图表数据 title, type
*
* @param charts
*/
protected void richingCharts(JSONArray charts) {
for (Iterator<Object> iter = charts.iterator(); iter.hasNext(); ) {
JSONObject ch = (JSONObject) iter.next();
ID chartid = ID.valueOf(ch.getString("chart"));
ConfigEntry e = getChart(chartid);
if (e == null) {
iter.remove();
continue;
}
ch.put("title", e.getString("title"));
ch.put("type", e.getString("type"));
}
}
@Test
public void testJSONToResult() {
String json = "[{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1.012\"}]";
try {
JSONArray array = JSON.parseArray(json);
// System.out.println(array);
Assert.assertEquals(array.toString(),
"[{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1.012\"}]");
List<TagResult> arrayList = new ArrayList<TagResult>(array.size());
Iterator<Object> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject object = (JSONObject) iterator.next();
Set<Entry<String, Object>> entrySet = object.entrySet();
for (Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Assert.assertNotEquals(key, null);
Object valueObject = entry.getValue();
Assert.assertNotEquals(key, null);
String value = valueObject.toString();
TagResult tagResult = new TagResult(key, value);
arrayList.add(tagResult);
break;
}
}
Assert.assertEquals(arrayList.size(), 3);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void fromArrayToBundle(Bundle bundle, String key, Object array) {
if (bundle != null && !TextUtils.isEmpty(key) && array != null) {
if (array instanceof String[]) {
bundle.putStringArray(key, (String[]) ((String[]) array));
} else if (array instanceof byte[]) {
bundle.putByteArray(key, (byte[]) ((byte[]) array));
} else if (array instanceof short[]) {
bundle.putShortArray(key, (short[]) ((short[]) array));
} else if (array instanceof int[]) {
bundle.putIntArray(key, (int[]) ((int[]) array));
} else if (array instanceof long[]) {
bundle.putLongArray(key, (long[]) ((long[]) array));
} else if (array instanceof float[]) {
bundle.putFloatArray(key, (float[]) ((float[]) array));
} else if (array instanceof double[]) {
bundle.putDoubleArray(key, (double[]) ((double[]) array));
} else if (array instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) ((boolean[]) array));
} else if (array instanceof char[]) {
bundle.putCharArray(key, (char[]) ((char[]) array));
} else {
if (!(array instanceof JSONArray)) {
throw new IllegalArgumentException("Unknown array type " + array.getClass());
}
ArrayList arraylist = new ArrayList();
JSONArray jsonArray = (JSONArray) array;
Iterator it = jsonArray.iterator();
while (it.hasNext()) {
JSONObject object = (JSONObject) it.next();
arraylist.add(fromJsonToBundle(object));
}
bundle.putParcelableArrayList(key, arraylist);
}
}
}
@Override
public void webWxGetContact() {
String url = String.format(URLEnum.WEB_WX_GET_CONTACT.getUrl(), core
.getLoginInfo().get(StorageLoginInfoEnum.url.getKey()));
Map<String, Object> paramMap = core.getParamMap();
HttpEntity entity = httpClient.doPost(url, JSON.toJSONString(paramMap));
try {
String result = EntityUtils.toString(entity, Consts.UTF_8);
// System.out.println("webWxGetContact:" + result);//CPP
JSONObject fullFriendsJsonList = JSON.parseObject(result);
// 查看seq是否为0,0表示好友列表已全部获取完毕,若大于0,则表示好友列表未获取完毕,当前的字节数(断点续传)
long seq = 0;
long currentTime = 0L;
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
if (fullFriendsJsonList.get("Seq") != null) {
seq = fullFriendsJsonList.getLong("Seq");
currentTime = new Date().getTime();
}
core.setMemberCount(fullFriendsJsonList
.getInteger(StorageLoginInfoEnum.MemberCount.getKey()));
JSONArray member = fullFriendsJsonList
.getJSONArray(StorageLoginInfoEnum.MemberList.getKey());
// 循环获取seq直到为0,即获取全部好友列表 ==0:好友获取完毕 >0:好友未获取完毕,此时seq为已获取的字节数
while (seq > 0) {
// 设置seq传参
params.add(new BasicNameValuePair("r", String
.valueOf(currentTime)));
params.add(new BasicNameValuePair("seq", String.valueOf(seq)));
entity = httpClient.doGet(url, params, false, null);
params.remove(new BasicNameValuePair("r", String
.valueOf(currentTime)));
params.remove(new BasicNameValuePair("seq", String.valueOf(seq)));
result = EntityUtils.toString(entity, Consts.UTF_8);
fullFriendsJsonList = JSON.parseObject(result);
if (fullFriendsJsonList.get("Seq") != null) {
seq = fullFriendsJsonList.getLong("Seq");
currentTime = new Date().getTime();
}
// 累加好友列表
member.addAll(fullFriendsJsonList
.getJSONArray(StorageLoginInfoEnum.MemberList.getKey()));
}
core.setMemberCount(member.size());
for (Iterator<?> iterator = member.iterator(); iterator.hasNext();) {
JSONObject o = (JSONObject) iterator.next();
if ((o.getInteger("VerifyFlag") & 8) != 0) { // 公众号/服务号
core.getPublicUsersList().add(o);
} else if (Config.API_SPECIAL_USER.contains(o
.getString("UserName"))) { // 特殊账号
core.getSpecialUsersList().add(o);
} else if (o.getString("UserName").indexOf("@@") != -1) { // 群聊
if (!core.getGroupIdList()
.contains(o.getString("UserName"))) {
core.getGroupNickNameList()
.add(o.getString("NickName"));
core.getGroupIdList().add(o.getString("UserName"));
core.getGroupList().add(o);
}
} else if (o.getString("UserName").equals(
core.getUserSelf().getString("UserName"))) { // 自己
core.getContactList().remove(o);
} else { // 普通联系人
core.getContactList().add(o);
}
}
return;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return;
}