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資料任君挑選!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM