package com.alibaba.fastjson;

import java.util.Date;
import java.util.List; import com.alibaba.fastjson.componet.Grade;
import com.alibaba.fastjson.componet.User;
import com.alibaba.fastjson.serializer.SerializerFeature; /**
* @author Liang
*
* 2017年2月27日
*/
public class JSONObject_ { public static void main(String[] args) {
User lime = new User(1, "lime", 23d);
User oracle = new User(2, "oracle", 25d);
Grade grade = new Grade("铃兰一中", lime, oracle); // 将JavaBean序列化为JSON文本
String limeJson = JSONObject.toJSONString(lime);
// {"id":1,"name":"lime","treasure":23}
String usersJson = JSONObject.toJSONString(grade.getUsers());
// [{"id":1,"name":"lime","treasure":23},{"id":2,"name":"oracle","treasure":25}]
String gradeJson = JSONObject.toJSONString(grade);
// {"name":"铃兰一中","users":[{"id":1,"name":"lime","treasure":23},{"id":2,"name":"oracle","treasure":25}]} // JSONObject 其实就是一个Map。
JSONObject limeParse = JSONObject.parseObject(limeJson);
System.out.println(limeParse);
// {"id":1,"name":"lime","treasure":23}
System.out.println(limeParse.getInteger("id"));
//
System.out.println(limeParse.getIntValue("id"));
//
System.out.println(limeParse.getString("name"));
// lime JSONObject gradeParse = JSONObject.parseObject(gradeJson);
System.out.println(gradeParse);
// {"name":"铃兰一中","users":[{"id":1,"name":"lime","treasure":23},{"id":2,"name":"oracle","treasure":25}]}
System.out.println(gradeParse.get("users"));
// [{"id":1,"name":"lime","treasure":23},{"id":2,"name":"oracle","treasure":25}] // JSONArray 其实就是一个List
List<User> gradeUsersParse = JSONObject.parseArray(gradeParse.get("users").toString(), User.class);
for(User user : gradeUsersParse){
System.out.println(user);
// User [id=1, name=lime, treasure=23.0]
// User [id=2, name=oracle, treasure=25.0]
} // key-value使用单引号
String limeJSON = JSONObject.toJSONString(lime,
SerializerFeature.UseSingleQuotes);
// {'id':1,'name':'lime','treasure':23} // 日期格式化
Date date = new Date();
// 默认格式为yyyy-MM-dd HH:mm:ss
System.out.println(JSON.toJSONString(date,
SerializerFeature.WriteDateUseDateFormat));
//根据自定义格式输出日期
System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
}
}

啦啦啦

com.alibaba.fastjson.JSONObject的更多相关文章

  1. 探索RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to xxx

    今天使用RequestBody接受前端传过来的参数,以前接受字符串数组非常成功,这次把形参改成了List<User>,原本以为顺利接受参数并映射成User的list结构,结果竟然在我取us ...

  2. 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String

    json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...

  3. No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*

    1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等 @Path("/searchProductByText") @GET @Produces({"ap ...

  4. net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject

    文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...

  5. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

    从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...

  6. com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题

    1.今天定义了一个JSONObject对象,引用的com.alibaba.fastjson.JSONObject,循环给这个对象赋值出现"$ref":"$[0]" ...

  7. com.alibaba.fastjson.JSONObject之对象与JSON转换方法

    com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...

  8. com.alibaba.fastjson.JSONObject;的使用

    转: com.alibaba.fastjson.JSONObject;的使用 2018-11-04 23:51:23 mameng1998 阅读数 6404更多 分类专栏: java   1  POM ...

  9. Java-Class-I:com.alibaba.fastjson.JSONObject

    ylbtech-Java-Class-I:com.alibaba.fastjson.JSONObject 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;imp ...

随机推荐

  1. 自适应瀑布型布局(手机,PC全兼容)

    有个瀑布型的布局需求,于是找了下,网上大部分用jquery控件,界面太复杂,看起来很笨重,我需求比较简单,于是只能自己写 其实原理就是用cs 去控制,那么我们只需要把对应的界面去规划就行了,比如,我一 ...

  2. C/C++中near和far的区别

    C/C++中near和far的区别 关键字near和far受目标计算机体系结构的影响.目前编程中使用不多. near关键字创建一个指向可寻址内存低端部分的目标指针.这些指针占用内存的单一字节,并且他们 ...

  3. Vue路由获取路由参数

    vue路由设置路由参数有2种方式: 1.通过query配置: <router-link :to="{ name:'login',query:{id:1} }">登录&l ...

  4. android 图片处理

    一.缩放 指宽.高缩放 (1)按比例缩放 在开发图片浏览器等软件是,很多时候要显示图片的缩略图,而一般情况下,我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的de ...

  5. 第三百四十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过downloadmiddleware中间件全局随机更换user-agent浏览器用户代理

    第三百四十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过downloadmiddleware随机更换user-agent浏览器用户代理 downloadmiddleware介绍中间件是 ...

  6. e825. 当JSplitPane改变大小时分配空间

    The weight of a split pane controls the behavior of the divider when the split pane is resized. If t ...

  7. tpshop模板

    TPshop模板在根目录 的 Template 下面 要修改某个模块下面的模板路径 修改 对应模块下面的Conf/html.php 文件的 <?php return array( 'HTML_C ...

  8. Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建

    Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建 Nginx 是一个高性能的 HTTP反向代理服务器 Keepalived 是一个基于VRRP协议来实现的LVS服务高可 ...

  9. mysql错误代码对照表较完整 mysql_errno()

    From: http://blog.csdn.net/aidenliu/article/details/5925604 mysql错误代码对照表较完整  0101 属于其他进程的专用标志. 0102 ...

  10. Git log和git reflog

    1.git log  log命令可以显示所有提交过的版本信息.显示信息如下: $ git log commit e1bdff6e4830e09383078c860f45334d03771b03 (HE ...