Java Code Examples for com.alibaba.fastjson.JSONObject#toJavaObject()

The following examples show how to use com.alibaba.fastjson.JSONObject#toJavaObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JedisScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceItem get(String queueID, String key) {
    if (!lockQueue(queueID)) {
        return null;
    }
    Jedis jedis = jedisPool.getResource();
    try {
        String dataJson = jedis.hget(makeDataKey(queueID), key);
        if (isNil(dataJson)) {
            return null;
        }
        return JSONObject.toJavaObject(JSON.parseObject(dataJson), ResourceItem.class);
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 2
Source File: BaofooCheckResp.java    From aaden-pay with Apache License 2.0 6 votes vote down vote up
public static BaofooCheckResp parse(String retStr) {
	if (retStr != null) {
		JSONObject json = new JSONObject();
		String[] arr = retStr.split("&");
		for (String string : arr) {
			String[] keyValue = string.split("=", 2);
			String key = keyValue[0];
			String value = null;
			if (keyValue.length < 2) {
				value = "";
			} else {
				value = keyValue[1];
			}				
			json.put(key, value);
		}
		return json.toJavaObject(BaofooCheckResp.class);
	}
	return new BaofooCheckResp();
}
 
Example 3
Source File: RpcClient.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @description 获取远程节点列表
 * @return 节点信息
 */
public List<PeerResult> getPeerInfo() {
    RpcRequest postData = getPostData(RpcMethod.GET_PEER_INFO);
    String result = HttpUtil.httpPostBody(getUrl(), postData.toJsonString());
    if (StringUtil.isNotEmpty(result)) {
        JSONObject parseObject = JSONObject.parseObject(result);
        if (messageValidate(parseObject))
            return null;
        JSONObject resultJson = parseObject.getJSONObject("result");
        JSONArray jsonArray = resultJson.getJSONArray("peers");
        List<PeerResult> peerList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject peerJson = jsonArray.getJSONObject(i);
            PeerResult peer = JSONObject.toJavaObject(peerJson, PeerResult.class);
            peerList.add(peer);
        }
        return peerList;
    }
    return null;
}
 
Example 4
Source File: RpcClient.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @description 获取最新的区块头 GetLastHeader
 * @return 最新区块信息
 */
public BlockResult getLastHeader() {
    RpcRequest postData = getPostData(RpcMethod.GET_LAST_HEADER);
    String result = HttpUtil.httpPostBody(getUrl(), postData.toJsonString());
    if (StringUtil.isNotEmpty(result)) {
        JSONObject parseObject = JSONObject.parseObject(result);
        if (messageValidate(parseObject))
            return null;
        JSONObject jsonResult = parseObject.getJSONObject("result");
        BlockResult block = JSONObject.toJavaObject(jsonResult, BlockResult.class);
        Long blockTimeLong = jsonResult.getLong("blockTime");
        block.setBlockTime(new Date(blockTimeLong * 1000));
        return block;
    }
    return null;
}
 
Example 5
Source File: SudaPlatformInterceptor.java    From charging_pile_cloud with MIT License 6 votes vote down vote up
/**
 * 管理员登陆校验
 *
 * @param request
 * @param response
 * @param path
 * @param token
 * @return
 */
private Boolean processAdmin(HttpServletRequest request,HttpServletResponse response, String path,String token) {
    Long id = AuthSign.getUserId(token);
    //获取存储sessionId
    String sessionId = userCacheUtil.getStoreAdminUser(id);
    if(sessionId==null){
        ResponseUtil.out(response, ResponseUtil.getLoginOutTimeMap());
        return false;
    }
    if(!token.equalsIgnoreCase(sessionId)){
        ResponseUtil.out(response, ResponseUtil.getLoginOutResponseMap());
        return false;
    }
    //用户禁用启用状态
    String jsonUser =  redisUtils.getStorageAdminUser(id);
    if(jsonUser != null){
        AdminUser adminUser = JSONObject.toJavaObject(JSONObject.parseObject(jsonUser), AdminUser.class);
        if(adminUser.getIsDisable()!=null) {
            if (!adminUser.getIsDisable()) {
                return false;
            }
        }
    }
    userCacheUtil.storeAdminUserRefreshExpire(id);
    return true;
}
 
Example 6
Source File: UserService.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 验证用户md5
 *
 * @param userMd5 用户md5
 * @return userModel 用户对象
 */
public UserModel checkUser(String userMd5) {
    JSONObject jsonData = getJSONObject(ServerConfigBean.USER);
    if (jsonData == null) {
        return null;
    }
    for (String strKey : jsonData.keySet()) {
        JSONObject jsonUser = jsonData.getJSONObject(strKey);
        UserModel userModel = jsonUser.toJavaObject(UserModel.class);
        String strUserMd5 = userModel.getUserMd5Key();
        if (strUserMd5.equals(userMd5)) {
            return userModel;
        }
    }
    return null;
}
 
Example 7
Source File: RpcClient.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @description 设置地址标签
 * 
 * @param addr  例如 13TbfAPJRmekQxYVEyyGWgfvLwTa8DJW6U
 * @param label 例如 macAddrlabel
 * @return 结果
 */
public AccountResult setlabel(String addr, String label) {
    RpcRequest postData = getPostData(RpcMethod.SET_LABEL);
    JSONObject requestParam = new JSONObject();
    requestParam.put("addr", addr);
    requestParam.put("label", label);
    postData.addJsonParams(requestParam);
    String result = HttpUtil.httpPostBody(getUrl(), postData.toJsonString());
    if (StringUtil.isNotEmpty(result)) {
        JSONObject parseObject = JSONObject.parseObject(result);
        if (messageValidate(parseObject))
            return null;
        JSONObject resultJson = parseObject.getJSONObject("result");
        AccountResult accountResult = resultJson.toJavaObject(AccountResult.class);
        return accountResult;
    }
    return null;
}
 
Example 8
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceItem remove(String queueID, String key) {
    if (!lockQueue(queueID)) {
        return null;
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        String dataJson = jedis.hget(makeDataKey(queueID), key);
        if (isNil(dataJson)) {
            return null;
        }
        jedis.hdel(makeDataKey(queueID), key);
        // lrem很消耗资源,尽量减少该命令操作
        for (String slice : sliceQueue(queueID)) {
            if (jedis.lrem(makePoolQueueKey(queueID, slice), 1, key) > 0) {
                break;
            }
        }
        return JSONObject.toJavaObject(JSON.parseObject(dataJson), ResourceItem.class);
    } finally {
        unLockQueue(queueID);
    }
}
 
Example 9
Source File: ProtocolUtil.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/**
 * 从报文中获取指定的对象
 *
 * @param jsonString
 * @param c
 * @param <T>
 * @return
 */
public static <T> T getObject(String jsonString, Class<T> c) {
    SvcCont svcCont = getSvcCont(jsonString);
    if (svcCont == null || svcCont.getObjs() == null || svcCont.getObjs().size() == 0) {
        return null;
    }
    for (JSONObject obj : svcCont.getObjs()) {
        if (obj !=null && c.getName().equals(obj.getString("beanName"))) {
            return JSONObject.toJavaObject(obj, c);
        }
    }
    return null;
}
 
Example 10
Source File: NettyServerHandler.java    From netty-learning-example with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, String request) throws Exception {
    try {
        JSONObject jsonObject = JSONObject.parseObject(request);
        Device device = jsonObject.toJavaObject(Device.class);
        deviceService.saveDevice(device);
        ctx.write("Successfully saved!\r\n");
    } catch (Exception e) {
        ctx.write("error Json format!\r\n");
        e.printStackTrace();
    }

}
 
Example 11
Source File: UserManager2.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
@Override
public User allocateUser() {
    ResourceItem resourceItem = resourceManager.allocate(vsCrawlerContext.makeUserResourceTag());
    if (resourceItem == null) {
        return null;
    }
    JSONObject jsonObject = JSONObject.parseObject(resourceItem.getData());
    return JSONObject.toJavaObject(jsonObject, User.class);
}
 
Example 12
Source File: RpcServerStreamHandler.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject msg) {
    if (MessageType.isRequestStream(msg)) {
        LOG.error("RequestStream: Not supported yet!");
    } else if (MessageType.isResponseStream(msg)) {
        try {
            StreamFragmentBean streamFragmentBean = msg.toJavaObject(StreamFragmentBean.class);
            MsgIdHolder.set(streamFragmentBean.getHeader().getMsgId());
            String ssid = streamFragmentBean.getHeader().getId();
            NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
            LocalNodeManager.handleMap.invalidate(ssid);
            //以下写出不会阻塞
            Stream stream = StreamManager.singleton.add(streamFragmentBean);
            if (streamFragmentBean.getHeader().isFirst()) {
                UnitResponse unitResponse = UnitResponse.createSuccess(stream);
                ThreadPoolManager.execute(() -> handler.callback(unitResponse));
            }
        } catch (Throwable e) {
            LOG.error(e);
        } finally {
            MsgIdHolder.clear();
        }
        // the pipeline chain ends here.
    } else {
        ctx.fireChannelRead(msg);
        //the pipeline chain continues.
    }
}
 
Example 13
Source File: ArticleConvertFactory.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 5 votes vote down vote up
private static ThreadPageInfo buildThreadPageInfo(JSONObject obj) {
    JSONObject subObj = (JSONObject) obj.get("__T");
    if (subObj == null) {
        return null;
    }
    try {
        return JSONObject.toJavaObject(subObj, ThreadPageInfo.class);
    } catch (RuntimeException e) {
        NLog.e(TAG, subObj.toJSONString());
    }
    return null;
}
 
Example 14
Source File: BaseDataService.java    From Jpom with MIT License 5 votes vote down vote up
protected <T> T getJsonObjectById(String file, String id, Class<T> cls) {
    if (StrUtil.isEmpty(id)) {
        return null;
    }
    JSONObject jsonObject = getJSONObject(file);
    if (jsonObject == null) {
        return null;
    }
    jsonObject = jsonObject.getJSONObject(id);
    if (jsonObject == null) {
        return null;
    }
    return jsonObject.toJavaObject(cls);
}
 
Example 15
Source File: BaseServiceDao.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/**
 * 校验请求报文,返回去Map
 *
 * @param jsonParam
 * @return
 */
public Map<String, Object> simpleValidateJSONReturnMap(String jsonParam) {
    JSONObject reqJson = null;
    Map<String, Object> reqMap = null;
    try {
        reqJson = JSONObject.parseObject(jsonParam);
        reqMap = JSONObject.toJavaObject(reqJson, Map.class);
    } catch (Exception e) {
        //抛出转json异常
        throw new RuntimeException(SERVICE_CASE_JSON_EXCEPTION+"请求报文格式错误String无法转换为JSONObjcet对象:", e);
    } finally {
        LoggerEngine.debug("报文简单校验simpleValidateJSON结束,出参为:" , reqMap);
    }
    return reqMap;
}
 
Example 16
Source File: SshInstallAgentController.java    From Jpom with MIT License 5 votes vote down vote up
private Object getNodeModel(String data) {
    NodeModel nodeModel = JSONObject.toJavaObject(JSONObject.parseObject(data), NodeModel.class);
    if (StrUtil.isEmpty(nodeModel.getId())) {
        return new JsonMessage<>(405, "节点id错误");
    }
    if (StrUtil.isEmpty(nodeModel.getName())) {
        return new JsonMessage<>(405, "输入节点名称");
    }
    if (StrUtil.isEmpty(nodeModel.getUrl())) {
        return new JsonMessage<>(405, "请输入节点地址");
    }
    return nodeModel;
}
 
Example 17
Source File: TagsComponent.java    From weixin4j with Apache License 2.0 5 votes vote down vote up
/**
 * 创建标签
 *
 * <p>
 * 一个公众号,最多可以创建100个标签。</p>
 *
 * @param name 标签名,UTF8编码
 * @return 包含标签ID的对象
 * @throws org.weixin4j.WeixinException 微信操作异常
 */
public Tag create(String name) throws WeixinException {
    //创建请求对象
    HttpsClient http = new HttpsClient();
    //拼接参数
    JSONObject postTag = new JSONObject();
    JSONObject postName = new JSONObject();
    postName.put("name", name);
    postTag.put("tag", postName);
    //调用获创建标签接口
    Response res = http.post("https://api.weixin.qq.com/cgi-bin/tags/create?access_token=" + weixin.getToken().getAccess_token(), postTag);
    //根据请求结果判定,是否验证成功
    JSONObject jsonObj = res.asJSONObject();
    //成功返回如下JSON:
    //{"tag":{"id":134, name":"广东"}}
    if (jsonObj != null) {
        if (Configuration.isDebug()) {
            System.out.println("获取/tags/create返回json:" + jsonObj.toString());
        }
        Object errcode = jsonObj.get("errcode");
        if (errcode != null && !errcode.toString().equals("0")) {
            //返回异常信息
            throw new WeixinException(getCause(jsonObj.getIntValue("errcode")));
        } else {
            JSONObject tagJson = jsonObj.getJSONObject("tag");
            if (tagJson != null) {
                return JSONObject.toJavaObject(tagJson, Tag.class);
            }
        }
    }
    return null;
}
 
Example 18
Source File: ActorRegisterAction.java    From Almost-Famous with MIT License 5 votes vote down vote up
@Override
public Resoult execute(JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
    Account account = jsonObject.toJavaObject(Account.class);
    if (this.accountService.register(account)) {
        return Resoult.ok(RegisterProtocol.ACTOR_REGISTER_ACTION_RESP);
    }
    return Resoult.error(RegisterProtocol.ACTOR_REGISTER_ACTION_RESP, ErrorCode.SERVER_ERROR, "");
}
 
Example 19
Source File: RdbEventRecordHandler.java    From DataLink with Apache License 2.0 5 votes vote down vote up
private String getFamilyName(MediaMappingInfo mediaMappingInfo) {
    HBaseMappingParameter parameter = JSONObject.toJavaObject(mediaMappingInfo.getParameterObj(), HBaseMappingParameter.class);
    if (parameter != null) {
        return StringUtils.isNotBlank(parameter.getFamilyName()) ? parameter.getFamilyName() : "default";
    } else {
        return "default";
    }
}
 
Example 20
Source File: Bean.java    From xian with Apache License 2.0 4 votes vote down vote up
static <T extends Bean> T jsonToBean(JSONObject json, Class<T> clazz) {
    return json.toJavaObject(clazz);
}