本文整理汇总了Java中com.alibaba.fastjson.JSON.toJSON方法的典型用法代码示例。如果您正苦于以下问题:Java JSON.toJSON方法的具体用法?Java JSON.toJSON怎么用?Java JSON.toJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.JSON
的用法示例。
在下文中一共展示了JSON.toJSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_annoation
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_annoation() throws Exception {
Bob bob = new Bob("Bob", 30, true);
// JSONObject obj = (JSONObject) JSON.toJSON(bob);
// assertEquals(3, obj.size());
// assertEquals(Boolean.TRUE, obj.get("sex"));
// assertEquals("Bob", obj.get("name"));
// assertEquals(new Integer(30), obj.get("age"));
PersonInfo info = Bob.class.getAnnotation(PersonInfo.class);
JSONObject obj = (JSONObject) JSON.toJSON(info);
assertEquals(3, obj.size());
assertEquals(Boolean.TRUE, obj.get("sex"));
assertEquals("Bob", obj.get("name"));
assertEquals(new Integer(30), obj.get("age"));
}
示例2: viewInput
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@RequestMapping("/view.html")
public String viewInput(String payOrderId, ModelMap model) {
PayOrder item = null;
if(StringUtils.isNotBlank(payOrderId)) {
item = payOrderService.selectPayOrder(payOrderId);
}
if(item == null) {
item = new PayOrder();
model.put("item", item);
return "pay_order/view";
}
JSONObject object = (JSONObject) JSON.toJSON(item);
if(item.getPaySuccTime() != null) object.put("paySuccTime", DateUtil.date2Str(new Date(item.getPaySuccTime())));
if(item.getLastNotifyTime() != null) object.put("lastNotifyTime", DateUtil.date2Str(new Date(item.getLastNotifyTime())));
if(item.getExpireTime() != null) object.put("expireTime", DateUtil.date2Str(new Date(item.getExpireTime())));
if(item.getAmount() != null) object.put("amount", AmountUtil.convertCent2Dollar(item.getAmount()+""));
model.put("item", object);
return "pay_order/view";
}
示例3: viewInput
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@RequestMapping("/view.html")
public String viewInput(String orderId, ModelMap model) {
MchNotify item = null;
if(StringUtils.isNotBlank(orderId)) {
item = mchNotifyService.selectMchNotify(orderId);
}
if(item == null) {
item = new MchNotify();
model.put("item", item);
return "mch_notify/view";
}
JSONObject object = (JSONObject) JSON.toJSON(item);
if(item.getCreateTime() != null) object.put("createTime", DateUtil.date2Str(item.getCreateTime()));
if(item.getUpdateTime() != null) object.put("updateTime", DateUtil.date2Str(item.getUpdateTime()));
if(item.getLastNotifyTime() != null) object.put("lastNotifyTime", DateUtil.date2Str(item.getLastNotifyTime()));
model.put("item", object);
return "mch_notify/view";
}
示例4: lastdp
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public List<LastDPValue> lastdp(Collection<Timeline> timelines) throws HttpUnknowStatusException {
Object timelinesJSON = JSON.toJSON(timelines);
JSONObject obj = new JSONObject();
obj.put("queries", timelinesJSON);
String jsonString = obj.toJSONString();
HttpResponse httpResponse = httpclient.post(HttpAPI.QUERY_LASTDP, jsonString);
ResultResponse resultResponse = ResultResponse.simplify(httpResponse, this.httpCompress);
HttpStatus httpStatus = resultResponse.getHttpStatus();
switch (httpStatus) {
case ServerSuccessNoContent:
return null;
case ServerSuccess:
String content = resultResponse.getContent();
List<LastDPValue> queryResultList = JSON.parseArray(content, LastDPValue.class);
return queryResultList;
case ServerNotSupport:
throw new HttpServerNotSupportException(resultResponse);
case ServerError:
throw new HttpServerErrorException(resultResponse);
default:
throw new HttpUnknowStatusException(resultResponse);
}
}
示例5: viewInput
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@RequestMapping("/view.html")
public String viewInput(String refundOrderId, ModelMap model) {
RefundOrder item = null;
if(StringUtils.isNotBlank(refundOrderId)) {
item = refundOrderService.selectRefundOrder(refundOrderId);
}
if(item == null) {
item = new RefundOrder();
model.put("item", item);
return "refund_order/view";
}
JSONObject object = (JSONObject) JSON.toJSON(item);
if(item.getRefundSuccTime() != null) object.put("refundSuccTime", DateUtil.date2Str(item.getRefundSuccTime()));
if(item.getExpireTime() != null) object.put("expireTime", DateUtil.date2Str(item.getExpireTime()));
if(item.getRefundAmount() != null) object.put("amount", AmountUtil.convertCent2Dollar(item.getRefundAmount()+""));
model.put("item", object);
return "refund_order/view";
}
示例6: encryptionByName
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public static JSONObject encryptionByName(HttpSession session, Object data, Set<String> valueSet, boolean encryption) {
JSONObject req = (JSONObject) JSON.toJSON(data);
SerializerUtil serializer = new SerializerUtil(session, null, valueSet, encryption);
if(null != valueSet) {
return (JSONObject) req.parse(req.toJSONString(req, serializer.valuefilter));
}
return req;
}
示例7: test_1
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_1() throws Exception {
JSONObject user = new JSONObject();
user.put("id", 3);
user.put("name", "周访");
JSONObject json = (JSONObject) JSON.toJSON(user);
Assert.assertEquals(new Long(3), json.getLong("id"));
Assert.assertEquals("周访", json.getString("name"));
}
示例8: test_2
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_2() throws Exception {
HashMap user = new HashMap();
user.put("id", 3);
user.put("name", "周访");
JSONObject json = (JSONObject) JSON.toJSON(user);
Assert.assertEquals(new Long(3), json.getLong("id"));
Assert.assertEquals("周访", json.getString("name"));
}
示例9: test_3
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_3() throws Exception {
List users = new ArrayList();
HashMap user = new HashMap();
user.put("id", 3);
user.put("name", "周访");
users.add(user);
JSONArray array = (JSONArray) JSON.toJSON(users);
JSONObject json = array.getJSONObject(0);
Assert.assertEquals(new Long(3), json.getLong("id"));
Assert.assertEquals("周访", json.getString("name"));
}
示例10: test_error
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_error() throws Exception {
C c = new C();
JSONException error = null;
try {
JSON.toJSON(c);
} catch (JSONException e) {
error = e;
}
Assert.assertNotNull(error);
}
示例11: test_ex
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_ex() throws Exception {
RuntimeException ex = new RuntimeException();
JSONObject object = (JSONObject) JSON.toJSON(ex);
JSONArray array = object.getJSONArray("stackTrace");
array.getJSONObject(0).put("lineNumber", null);
JSON.parseObject(object.toJSONString(), Exception.class);
}
示例12: test_page
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_page() throws Exception {
List<Post> postList = new ArrayList<Post>();
{
postList.add(new Post(1001));
}
Page<Post> page = new PageImpl(postList);
JSONObject obj = (JSONObject) JSON.toJSON(page);
Assert.assertNotNull(obj);
Assert.assertEquals(1, obj.getJSONArray("content").size());
}
示例13: test_checkObject
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_checkObject() {
Art origin = makeOrigin();
JSONObject articleObj = (JSONObject) JSON.toJSON(origin);
JSONObject dataObj = new JSONObject();
dataObj.put("art", articleObj);
Art other = dataObj.getObject("art", Art.class);// return null;
assertSame(origin, other); // test failed
}
示例14: test_for_issue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
Model model = new Model();
model.date = new Date(1483413683714L);
JSONObject obj = (JSONObject) JSON.toJSON(model);
assertEquals("{\"date\":\"2017-01-03 11:21:23\"}", obj.toJSONString());
}
示例15: test_for_issue
import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
Date now = new Date();
GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
gregorianCalendar.setTime(now);
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
String jsonString = JSON.toJSONString(calendar);
// success
calendar = JSON.parseObject(jsonString, XMLGregorianCalendar.class);
Object toJSON1 = JSON.toJSON(calendar); // debug看到是 Long 类型
// 所以这里会报错:
// error: java.lang.ClassCastException: java.lang.Long cannot be cast to com.alibaba.fastjson.JSONObject
//JSONObject jsonObject = (JSONObject) JSON.toJSON(calendar);
// 所以 这里肯定会报错, 因为 jsonObject 不是JSONObject类型
//calendar = jsonObject.toJavaObject(XMLGregorianCalendar.class);
List<XMLGregorianCalendar> calendarList = new ArrayList<XMLGregorianCalendar>();
calendarList.add(calendar);
calendarList.add(calendar);
calendarList.add(calendar);
Object toJSON2 = JSON.toJSON(calendarList); // debug 看到是 JSONArray 类型
// success: 通过 JSONArray.parseArray 方法可以正确转换
JSONArray jsonArray = (JSONArray) JSON.toJSON(calendarList);
jsonString = jsonArray.toJSONString();
List<XMLGregorianCalendar> calendarList1 = JSONArray.parseArray(jsonString, XMLGregorianCalendar.class);
// 通过 jsonArray.toJavaList 无法转换
// error: com.alibaba.fastjson.JSONException: can not cast to : javax.xml.datatype.XMLGregorianCalendar
List<XMLGregorianCalendar> calendarList2 = jsonArray.toJavaList(XMLGregorianCalendar.class);
assertNotNull(calendarList2);
assertEquals(3, calendarList2.size());
}