JSON

解析

1.

public PageData resolveArrayJson(String json) throws Exception {
PageData pd=new PageData();
List<String> list=new ArrayList<String>();
List<String> userIdList=new ArrayList<String>();
JSONArray jsonArray=(JSONArray)JSONSerializer.toJSON(json);
for (Object object : jsonArray) {
JSONObject jsonObject = JSONObject.fromObject(object);
Object value = jsonObject.get("value");
if(null==value){
pd.put("APPdictcode", jsonObject.get("dictcode"));
}
if(null!=value){
String type =jsonObject.get("type").toString();
if("label".equals(type)){
pd.put("APPtitle", jsonObject.get("value"));
}
if("person".equals(type)){
pd.put("APPapprover", jsonObject.get("value"));
}
if("img".equals(type)){
pd.put("APPimg", jsonObject.get("value"));
}
if("department".equals(type)){
pd.put("partId", jsonObject.get("value"));
}
if("total".equals(type)){
pd.put("totalPrice", jsonObject.get("value"));
}
if("select".equals(type)){
JSONArray jar=(JSONArray) jsonObject.get("value");
for(Object o:jar){
JSONObject jj = JSONObject.fromObject(o);
list.add(jj.getString("states"));
userIdList.add(jj.getString("userId"));
}
pd.put("userIdList", userIdList);
}
}
}
for(String s:list){
if(("0").equals(s)){
pd.put("APPstates", "0");
break;
}else{
pd.put("APPstates", "1");
}
}
return pd;
}

2.

public Map<String,String> findRingGroupName(String groupId) throws Exception {
Map<String,String> map = new HashMap<String,String>();
String groupIds[] = new String[1];
groupIds[0] = groupId;
ObjectNode findGroup = EasemobChatGroups.getGroupDetailsByChatgroupid(groupIds);
ArrayNode dataArray = (ArrayNode) findGroup.get("data");
//获得群名称
ObjectNode dataJson = (ObjectNode) dataArray.get(0);
String groupName = dataJson.get("name").toString().replaceAll(""","");
//获得群主
ArrayList groupaFfiliations = (ArrayList) dataArray.findValues("affiliations");
ArrayNode groupArray = (ArrayNode) groupaFfiliations.get(0);
String groupOwner = "";
for (JsonNode jsonNode : groupArray) {
if(jsonNode.toString().indexOf("owner") != -1){
groupOwner = jsonNode.toString().replaceAll(""","");
}
}
map.put("groupName", groupName);
map.put("groupOwner", groupOwner);
return map;

}

封装

1.

//APP用户登陆成功后得到的JSON
public String getLoginSuccessJson(PageData pd) throws Exception{
JSONObject jsonMain = new JSONObject();
int i = 0;
TUser userApp;
try {
userApp = findUserByP(pd.getString("username"));
if(userApp != null){
List<PageData> orgList = findUserAllOrg(userApp.getUserId());
JSONArray deptArray = new JSONArray();
JSONArray frendsArray = new JSONArray();
//JSONArray usersArray = new JSONArray();
/*ObjectNode objectNode =EasemobIMUsers.getFriends(userApp.getUserPhone());
ArrayNode obj = (ArrayNode) objectNode.get("data");*/
//获取公司下所有用户
TUser user = new TUser();
user.setOrgId(ShiroUtil.getOrgId());
user.setUserId(ShiroUtil.getUserId());
List<PageData> userList = (List<PageData>) dao.findForList("UserMapper.findAllAppUser", user);
JSONArray usersArray = new JSONArray();
// ObjectNode objectNode =EasemobIMUsers.getFriends(userApp.getUserPhone());
// ArrayNode obj = (ArrayNode) objectNode.get("data");
//生成当前用户部门JSONArray
if(orgList.size()>0){
for(PageData p :orgList){
JSONObject jsonMainDept = new JSONObject();
jsonMainDept.put("orgid", p.getString("T_ORG_ID"));
jsonMainDept.put("orgname", p.getString("ORG_NAME"));
deptArray.add(i++, jsonMainDept);
}
}
jsonMain.put("status", "200");
jsonMain.put("companyid", ShiroUtil.getOrgId());
jsonMain.put("dept", deptArray);
jsonMain.put("tel", userApp.getUserPhone());
jsonMain.put("email", userApp.getUserEmail());
jsonMain.put("nickname", userApp.getUserNickname());
jsonMain.put("userid", userApp.getUserId());
jsonMain.put("companyid", userApp.getOrgId());
jsonMain.put("position", findUserById(userApp.getUserId()).getPositionName());
//生成好友JSONArray
if(userList.size()>0){
int f = 0;
for(PageData pp: userList){
//String userPhone = a.toString().replaceAll(""", "");
TUser friendUser = findUserByP(pp.getString("USER_PHONE"));
List<PageData> frendOrgList = findUserAllOrg(friendUser.getUserId());
JSONObject jsonfrendMain = new JSONObject();
JSONArray frendDeptArray = new JSONArray();
//生成好友的部门JSONArray
int fd = 0;
if(frendOrgList.size()>0){
for(PageData p :frendOrgList){
JSONObject jsonFriendMainDept = new JSONObject();
jsonFriendMainDept.put("orgid", p.getString("T_ORG_ID"));
jsonFriendMainDept.put("orgname", p.getString("ORG_NAME"));
frendDeptArray.add(fd++, jsonFriendMainDept);
}
}
jsonfrendMain.put("userid", friendUser.getUserId());
jsonfrendMain.put("nickname", friendUser.getUserNickname());
jsonfrendMain.put("headicon", friendUser.getUserIcon());
jsonfrendMain.put("dept", frendDeptArray);
jsonfrendMain.put("tel", friendUser.getUserPhone());
jsonfrendMain.put("email", friendUser.getUserEmail());
jsonfrendMain.put("position", findUserById(friendUser.getUserId()).getPositionName());
jsonfrendMain.put("emailture", friendUser.getUserEmail());
frendsArray.add(f++, jsonfrendMain);
}
}
jsonMain.put("friends",frendsArray);

return jsonMain.toString();
}else{
jsonMain.put("status", 400);
jsonMain.put("msg", "can't find user");
return jsonMain.toString();
}
} catch (Exception e) {
jsonMain.put("status", 400);
jsonMain.put("msg", "error");
e.printStackTrace();
return jsonMain.toString();
}
}

原文地址:https://www.cnblogs.com/jianyi12/p/5629789.html