本文整理汇总了Java中com.alibaba.fastjson.JSON.toJavaObject方法的典型用法代码示例。如果您正苦于以下问题:Java JSON.toJavaObject方法的具体用法?Java JSON.toJavaObject怎么用?Java JSON.toJavaObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.JSON
的用法示例。
在下文中一共展示了JSON.toJavaObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_for_issue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
Money money = new Money();
money.currency = Currency.getInstance("CNY");
money.amount = new BigDecimal("10.03");
String json = JSON.toJSONString(money);
Money moneyBack = JSON.parseObject(json, Money.class);
Assert.assertEquals(money.currency, moneyBack.currency);
Assert.assertEquals(money.amount, moneyBack.amount);
JSONObject jsonObject = JSON.parseObject(json);
Money moneyCast = JSON.toJavaObject(jsonObject, Money.class);
Assert.assertEquals(money.currency, moneyCast.currency);
Assert.assertEquals(money.amount, moneyCast.amount);
}
示例2: test_3
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_3() throws Exception {
JSONObject map = new JSONObject();
map.put("id", 1);
map.put("name", "panlei");
User user = JSON.toJavaObject(map, User.class);
Assert.assertEquals(1L, user.getId());
Assert.assertEquals("panlei", user.getName());
}
示例3: testJson
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void testJson() throws Exception {
Money money = new Money();
money.currency = Currency.getInstance("CNY");
money.amount = new BigDecimal("10.03");
String json = JSON.toJSONString(money);
System.out.println("json = " + json);
Money moneyBack = JSON.parseObject(json, Money.class);
System.out.println("money = " + moneyBack);
JSONObject jsonObject = JSON.parseObject(json);
Money moneyCast = JSON.toJavaObject(jsonObject, Money.class);
System.out.printf("money = " + moneyCast);
}
示例4: test_for_issue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
String json = "{\n" +
" \"info\": {\n" +
" \"test\": \"\", \n" +
" }\n" +
"}";
JSONObject jsonObject = JSON.parseObject(json);
TestModel loginResponse = JSON.toJavaObject(jsonObject, TestModel.class);
}
示例5: deserialze
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONObject json = parser.parseObject();
int num = json.getInteger("num");
if (num == 1) {
return (T) JSON.toJavaObject(json, B.class);
} else if (num == 2) {
return (T) JSON.toJavaObject(json, C.class);
} else {
return (T) JSON.toJavaObject(json, A.class);
}
}
示例6: test_for_issue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
TestObject to = new TestObject();
to.add("test1");
to.add("test2");
String text = JSON.toJSONString(to);
System.out.println(text);
JSONObject jo = JSON.parseObject(text);
to = JSON.toJavaObject(jo, TestObject.class);
}
示例7: test_for_bug
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_bug() throws Exception {
Entity entity = new Entity();
entity.setHolder(new Holder<String>("AAA"));
JSONObject json = (JSONObject) JSON.toJSON(entity);
Entity entity2 = JSON.toJavaObject(json, Entity.class);
Assert.assertEquals(JSON.toJSONString(entity), JSON.toJSONString(entity2));
}
示例8: getJsonObj
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static <T> T getJsonObj(Class<T> c, JSON content) {
try {
return JSON.toJavaObject(content, c);
} catch (Exception e) {
if (e != null) {
Logger.d("fornia", "e.getMessage():" + e.getMessage());
}
return null;
}
}
示例9: getJsonObj
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static <T> T getJsonObj(Class<T> c, JSON content) {
try {
return JSON.toJavaObject(content, c);
} catch (Exception e) {
if (e != null) {
LogInfo.log("fornia", "e.getMessage():" + e.getMessage());
}
return null;
}
}
示例10: test_0
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_0() throws Exception {
A a = (A) JSON.toJavaObject(new JSONObject(), A.class);
Assert.assertNotNull(a);
}
示例11: toObject
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static <T> T toObject(Object jsonObject, Class<T> clz){
if (Objects.isNull(jsonObject))
return null;
return JSON.toJavaObject((JSON)jsonObject, clz);
}
示例12: transferStringToSeed
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static Seed transferStringToSeed(String seed) {
return JSON.toJavaObject(JSONObject.parseObject(seed), Seed.class);
}
示例13: doAliPayMobileReq
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public Map doAliPayMobileReq(String jsonParam) {
String logPrefix = "【支付宝APP支付下单】";
BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
Map<String, Object> bizParamMap = baseParam.getBizParamMap();
if (ObjectValidUtil.isInvalid(bizParamMap)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
}
JSONObject payOrderObj = baseParam.isNullValue("payOrder") ? null : JSONObject.parseObject(bizParamMap.get("payOrder").toString());
PayOrder payOrder = JSON.toJavaObject(payOrderObj, PayOrder.class);
if (ObjectValidUtil.isInvalid(payOrder)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
}
String payOrderId = payOrder.getPayOrderId();
String mchId = payOrder.getMchId();
String channelId = payOrder.getChannelId();
PayChannel payChannel = baseService4PayOrder.baseSelectPayChannel(mchId, channelId);
alipayConfig.init(payChannel.getParam());
AlipayClient client = new DefaultAlipayClient(alipayConfig.getUrl(), alipayConfig.getApp_id(), alipayConfig.getRsa_private_key(), AlipayConfig.FORMAT, AlipayConfig.CHARSET, alipayConfig.getAlipay_public_key(), AlipayConfig.SIGNTYPE);
AlipayTradeAppPayRequest alipay_request = new AlipayTradeAppPayRequest();
// 封装请求支付信息
AlipayTradeAppPayModel model=new AlipayTradeAppPayModel();
model.setOutTradeNo(payOrderId);
model.setSubject(payOrder.getSubject());
model.setTotalAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount().toString()));
model.setBody(payOrder.getBody());
model.setProductCode("QUICK_MSECURITY_PAY");
alipay_request.setBizModel(model);
// 设置异步通知地址
alipay_request.setNotifyUrl(alipayConfig.getNotify_url());
// 设置同步地址
alipay_request.setReturnUrl(alipayConfig.getReturn_url());
String payParams = null;
try {
payParams = client.sdkExecute(alipay_request).getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
baseService4PayOrder.baseUpdateStatus4Ing(payOrderId, null);
_log.info("{}生成请求支付宝数据,payParams={}", logPrefix, payParams);
_log.info("###### 商户统一下单处理完成 ######");
Map<String, Object> map = XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
map.put("payOrderId", payOrderId);
map.put("payParams", payParams);
return RpcUtil.createBizResult(baseParam, map);
}
示例14: doAliTransReq
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
/**
* 支付宝转账,文档:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
* @param jsonParam
* @return
*/
@Override
public Map doAliTransReq(String jsonParam) {
String logPrefix = "【支付宝转账】";
BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
Map<String, Object> bizParamMap = baseParam.getBizParamMap();
if (ObjectValidUtil.isInvalid(bizParamMap)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
}
JSONObject transOrderObj = baseParam.isNullValue("transOrder") ? null : JSONObject.parseObject(bizParamMap.get("transOrder").toString());
TransOrder transOrder = JSON.toJavaObject(transOrderObj, TransOrder.class);
if (ObjectValidUtil.isInvalid(transOrder)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
}
String transOrderId = transOrder.getTransOrderId();
String mchId = transOrder.getMchId();
String channelId = transOrder.getChannelId();
PayChannel payChannel = baseService4TransOrder.baseSelectPayChannel(mchId, channelId);
alipayConfig.init(payChannel.getParam());
AlipayClient client = new DefaultAlipayClient(alipayConfig.getUrl(), alipayConfig.getApp_id(), alipayConfig.getRsa_private_key(), AlipayConfig.FORMAT, AlipayConfig.CHARSET, alipayConfig.getAlipay_public_key(), AlipayConfig.SIGNTYPE);
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
model.setOutBizNo(transOrderId);
model.setPayeeType("ALIPAY_LOGONID"); // 收款方账户类型
model.setPayeeAccount(transOrder.getChannelUser()); // 收款方账户
model.setAmount(AmountUtil.convertCent2Dollar(transOrder.getAmount().toString()));
model.setPayerShowName("支付转账");
model.setPayeeRealName(transOrder.getUserName());
model.setRemark(transOrder.getRemarkInfo());
request.setBizModel(model);
Map<String, Object> map = new HashMap<>();
map.put("transOrderId", transOrderId);
map.put("isSuccess", false);
try {
AlipayFundTransToaccountTransferResponse response = client.execute(request);
if(response.isSuccess()) {
map.put("isSuccess", true);
map.put("channelOrderNo", response.getOrderId());
}else {
//出现业务错误
_log.info("{}返回失败", logPrefix);
_log.info("sub_code:{},sub_msg:{}", response.getSubCode(), response.getSubMsg());
map.put("channelErrCode", response.getSubCode());
map.put("channelErrMsg", response.getSubMsg());
}
} catch (AlipayApiException e) {
_log.error(e, "");
}
return RpcUtil.createBizResult(baseParam, map);
}
示例15: getAliTransReq
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public Map getAliTransReq(String jsonParam) {
String logPrefix = "【支付宝转账查询】";
BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
Map<String, Object> bizParamMap = baseParam.getBizParamMap();
if (ObjectValidUtil.isInvalid(bizParamMap)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
}
JSONObject transOrderObj = baseParam.isNullValue("transOrder") ? null : JSONObject.parseObject(bizParamMap.get("transOrder").toString());
TransOrder transOrder = JSON.toJavaObject(transOrderObj, TransOrder.class);
if (ObjectValidUtil.isInvalid(transOrder)) {
_log.warn("{}失败, {}. jsonParam={}", logPrefix, RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
}
String transOrderId = transOrder.getTransOrderId();
String mchId = transOrder.getMchId();
String channelId = transOrder.getChannelId();
PayChannel payChannel = baseService4PayOrder.baseSelectPayChannel(mchId, channelId);
alipayConfig.init(payChannel.getParam());
AlipayClient client = new DefaultAlipayClient(alipayConfig.getUrl(), alipayConfig.getApp_id(), alipayConfig.getRsa_private_key(), AlipayConfig.FORMAT, AlipayConfig.CHARSET, alipayConfig.getAlipay_public_key(), AlipayConfig.SIGNTYPE);
AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
AlipayFundTransOrderQueryModel model = new AlipayFundTransOrderQueryModel();
model.setOutBizNo(transOrderId);
model.setOrderId(transOrder.getChannelOrderNo());
request.setBizModel(model);
Map<String, Object> map = XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
map.put("transOrderId", transOrderId);
try {
AlipayFundTransOrderQueryResponse response = client.execute(request);
if(response.isSuccess()){
map.putAll((Map) JSON.toJSON(response));
map.put("isSuccess", true);
}else {
_log.info("{}返回失败", logPrefix);
_log.info("sub_code:{},sub_msg:{}", response.getSubCode(), response.getSubMsg());
map.put("channelErrCode", response.getSubCode());
map.put("channelErrMsg", response.getSubMsg());
}
} catch (AlipayApiException e) {
_log.error(e, "");
}
return RpcUtil.createBizResult(baseParam, map);
}