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. hbase 学习(十四)Facebook针对hbase的优化方案分析

    使用hbase的目的是为了海量数据的随机读写,但是在实际使用中却发现针对随机读的优化和gc是一个很大的问题,而且hbase的数据是存储在Hdfs,而Hdfs是面向流失数据访问进行设计的,就难免带来效率 ...

  2. Python Garbage Collection 与 Objective-C ARC

    转载请注明出处 http://blog.csdn.net/u014205968/article/details/69389363 Python GC 与 Objective-C ARC 提起GC(Ga ...

  3. python 安装pycurl

    yum install libghc-gnutls-dev opnssl nss -y 126 yum install libghc-gnutls-dev -y 127 yum install htt ...

  4. 七牛图片存储api

    https://developer.qiniu.com/kodo 属于对象存储 https://www.qiniu.com/prices 如何获取存储文件的外链接 https://developer. ...

  5. 关于源码输出,浏览器不解析Html标签

    有时候根据需要我们需要看到浏览器上源码效果如: 但是我如果在html中输入 <a href = 'http://www.baidu.com'>百度</a>那么问题来了,总是显示 ...

  6. jspSmartUpload使用初步

    jsp中使用SmartUpload上传文件 1.下载jspSmartUpload组件后,解压缩.把com目录复制到应用程序的WEB-INF\classes目录下. 2.尤其注意 (1)文件上传表单要添 ...

  7. backup & restore On Ubuntu

    详见:https://help.ubuntu.com/community/BackupYourSystem/TAR 在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过 ...

  8. 通过用户名、密码提交的方式搭建私有git服务端

    1. 系统环境 centos git yum -y install git 2).创建属于自己的git库 mkdir /myGit git init --bare xiangyun.git 3). 添 ...

  9. nodejs基础 -- web模块

    什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客 ...

  10. Tomcat介绍 安装jdk 安装Tomcat

    Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发而 ...