ObjectNode ArrayNode JsonNode
private static JsonNodeFactory factory = new JsonNodeFactory(false); ObjectNode objectNode = factory.objectNode(); ObjectNode dataObjectNode = JsonNodeFactory.instance.objectNode(); ArrayNode members = JsonNodeFactory.instance.arrayNode();
ArrayNode extends ContainerNode
ContainerNode extends ObjectNode
eg(调用的是云通讯,添加群组成员):
public static String addUserGroupByTm(String groupid, String userprimarykey, int Silence) {
String result = "";
ObjectNode objectNode = factory.objectNode();
ObjectNode dataObjectNode = JsonNodeFactory.instance.objectNode();
// check appKey format
if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
LOGGER.error("Bad format of Appkey: " + APPKEY);
throw new BizException(ExceptionCode.MESSAGE_APPKEY_ERROR);
// objectNode.put("message", "Bad format of Appkey");
// return objectNode;
}
if (groupid == null || userprimarykey == null) {
throw new BizException(ExceptionCode.REQUEST_PARAMS_MISS, ExceptionCode.REQUEST_PARAMS_MISS_MSG);
}
ArrayNode members = JsonNodeFactory.instance.arrayNode();
members.add(objectNode.put("Member_Account", userprimarykey));
dataObjectNode.put("GroupId", groupid);
dataObjectNode.put("Member_Account", userprimarykey);
dataObjectNode.put("Silence", Silence);
dataObjectNode.put("MemberList", members);
try {
JerseyWebTarget webTarget = null;
String sig = SigUtils.getSig(Long.valueOf(Constants.TM_APP_ID),
Constants.TM_APP_IDENTIFIER);
Random r = new Random();
int ran = r.nextInt();
webTarget = EndPoints.TM_GROUP_TARGET.path("add_group_member").queryParam("usersig", sig)
.queryParam("identifier", Constants.TM_APP_IDENTIFIER).queryParam("sdkappid", Constants.TM_APP_ID)
.queryParam("random", String.valueOf(ran)).queryParam("contenttype", "json");
ObjectNode resNode = factory.objectNode();
resNode = JerseyUtils.sendRequestForTencent(webTarget, dataObjectNode, credential,
HTTPMethod.METHOD_POST, null);
int ErrorCode = resNode.get("ErrorCode").asInt();
JsonNode tempFlag = resNode.get("MemberList");
int y = tempFlag.path(0).get("Result").asInt();
//tempFlag
if (ErrorCode == 10019) {
throw new BizException("B-123555", "被添加用户的帐号不存在!");
} else if (y == 2) {
throw new BizException("B-456122", "已经是群成员!");
}else if (y == 1) {
result = "success";
} else {
Thread.currentThread().sleep(300);
}
} catch (Exception e) {
result = "error";
e.printStackTrace();
}
return result;
}
JsonNode有两个path方法(抽象),两个参数方法;一个用字符串取值,一个是int获取对象
Array与ArrayNode
Array是数组
ArrayNode与数组存储相同
关注公众号,回复c+兴趣的东西 24小时内即可领取学习。2T资料任君挑选!