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资料任君挑选!

转载于:https://www.cnblogs.com/wwwcf1982603555/p/9145614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值