com.alibaba.fastjson.JSONObject
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的更多相关文章
- 探索RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to xxx
今天使用RequestBody接受前端传过来的参数,以前接受字符串数组非常成功,这次把形参改成了List<User>,原本以为顺利接受参数并映射成User的list结构,结果竟然在我取us ...
- 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String
json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...
- No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*
1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等 @Path("/searchProductByText") @GET @Produces({"ap ...
- net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject
文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题
1.今天定义了一个JSONObject对象,引用的com.alibaba.fastjson.JSONObject,循环给这个对象赋值出现"$ref":"$[0]" ...
- com.alibaba.fastjson.JSONObject之对象与JSON转换方法
com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...
- com.alibaba.fastjson.JSONObject;的使用
转: com.alibaba.fastjson.JSONObject;的使用 2018-11-04 23:51:23 mameng1998 阅读数 6404更多 分类专栏: java 1 POM ...
- Java-Class-I:com.alibaba.fastjson.JSONObject
ylbtech-Java-Class-I:com.alibaba.fastjson.JSONObject 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;imp ...
随机推荐
- 给data设置数据
console.log(JSON.stringify(that.data.navigator[0].content) + "--____+" + JSON.stringify(th ...
- 微信小程序——wxParse使用方法
wxParse是一个微信小程序富文本解析组件.现在小程序里面自带了一个<rich-text>组件也能解析富文本,但是表现不尽人意.所以我还是采用的wxParse来解析富文本的. wxPar ...
- C#里的SubString和Convert.ToDateTime
1.C#里的SubString String.SubString(int index,int length) index:开始位置,从0开始 length:你要取的子字符串的长度 2.C#语言 ...
- Linux C++ 访问子目录以及里面的文件
#include <iostream> #include <fstream> #include <string> #include <vector> # ...
- SAP MM01 创建物料主数据 [关注公众号后回复MM01获取更多资料]
操作内容 物料主数据,适用于所有有物料编码物料相关信息的系统维护 业务流程 新项目设计冻结后—M公司 PD用-物料编码申请表D-BOM Material Number Application部门内部 ...
- fsck 工具 ——检查 与修复 Linux系统上的文件系统
可能由于昨天关电脑断电源的问题, 后来开机,直接出现如下界面: 心里想,不会吧,电脑怎么又出问题了吧(上周的时候,手贱,把装系统的硬盘分区设为了非活动分区,电脑就启动不来了,后来手动引导起来的:): ...
- e834. 设置JTabbedPane中卡片的位置
The tabs of a tabbed pane can be placed on one of the four edges of its container. By default, when ...
- Java是一门面向对象编程语言的理解
Java是一门面向对象编程语言. 不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. Java语言作为静态面向对象编程语言的 ...
- Python入门学习:网络刷博器爬虫
1.比较有趣,可以不断刷新指定的网址 2.源码: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import webbrowser as web imp ...
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...