使用FastJson转化Json格式
1.下载Jar包
http://repo1.maven.org/maven2/com/alibaba/fastjson/
2.将jar包导入工程
3.示例
package nc.testFastJson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
public class TestFastJson {
public static void main(String[] args) {
// java对象 转 json
People p1 = new People("people_1","Male",1);
String p1_Json = JSON.toJSONString(p1);
System.out.println(p1_Json.toString());
// json 转 java对象
String p2_Json = "{'name':'people_2','sex':'Male','age':2}";
People p2 = JSON.parseObject(p2_Json, People.class);
System.out.println(p2.toString());
// java对象LinkedList集合 转 json
LinkedList<People> p_list = new LinkedList<>();
People p3 = new People("people_3","Male",3);
People p4 = new People("people_4","Male",4);
People p5 = new People("people_5","Male",5);
p_list.add(p3);
p_list.add(p4);
p_list.add(p5);
String p_list_Json = JSON.toJSONString(p_list);
System.out.println(p_list_Json);
// json 转 java对象List集合
List<People> p_list_2 = JSON.parseArray(p_list_Json, People.class);
for (People people : p_list_2) {
System.out.println(people.toString());
}
// java对象ArrayList 转 json
ArrayList<People> arrayList = new ArrayList<>();
arrayList.add(p3);
arrayList.add(p4);
arrayList.add(p5);
String arrays_json = JSON.toJSONString(arrayList);
System.out.println(arrays_json);
// json 转 java对象List集合
List<People> arrayList2 = JSON.parseArray(arrays_json, People.class);
for (People people : arrayList2) {
System.out.println(people.toString());
}
// map 转 json
HashMap<String ,People> map = new HashMap<>();
map.put("p3", p3);
map.put("p4", p4);
map.put("p5", p5);
String map_json = JSON.toJSONString(map);
System.out.println(map_json);
// json 转 map
Map<String, String> map2 = JSONObject.parseObject(map_json.toString(), new TypeReference<Map<String, String>>(){});
Set<Entry<String, String>> entrySet = map2.entrySet();
for (Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue();
People p = JSON.parseObject(value, People.class);
System.out.println(key+":"+p.toString());
}
}
}
package nc.testFastJson;
public class People {
private String name ;
private String sex ;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
public People() {
super();
}
public People(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
}

使用FastJson转化Json格式的更多相关文章
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...
- 如何利用fastjson将JSON格式的字符串转换为Map,再返回至前端成为js对象
//注意,这里的jsonStr是json格式的字符串,里面如果遇到双引号嵌套双引号的,一般是嵌套的双引号经过转义 // \",假如有这样的一个场景,这些字符串里面有需要的css样式的j ...
- fastjson将json格式null转化空串
生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.pu ...
- fastjson将json格式字符串转成list集合
1.gameListStr = "[{"gameId":"1","gameName":"哈哈"},{" ...
- FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换
当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('g ...
- SpringBoot实体类对象和json格式的转化
1.引入maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson ...
- Java基础/利用fastjson反序列化json为对象和对象数组
利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...
- 获取JSON格式的字符串各个属性对应的值
{"lastrdtime":1515998187379,"creditbalance":"$5.00","contactmode& ...
- fastjson处理json
返回主页 你是风儿 博客园首页新随笔联系订阅管理 随笔 - 29 文章 - 0 评论 - 23 FastJson对于JSON格式字符串.JSON对象及JavaBean之间的相互转换 fastJson对 ...
随机推荐
- 常用内置模块(二)——logging模块
logging模块 一.logging作用 1. 控制日志级别 2. 控制日志格式 3. 控制输出的目标为文件 二.日志级别 logging.debug( logging.info( loggin ...
- PHPExcel导出工作蒲(多表合并)教程+详细代码解读
最近做了一个需求,导出统计数据,因为需要同时导出多个不同的统计数据,所以不能像以往导出数据列表一样去实现这个需求,刚好空下来就记录一下(PHPExcel导出Excel多sheet合并) 一.主要使用的 ...
- Size Assert
判断返回内容的大小
- AVR446步进电机算法推导及应用
https://blog.csdn.net/Renjiankun/article/details/80513839?utm_source=copy
- mongodb入门篇
MongoDB 入门篇 分类: NoSQL, 故障解决 undefined 1.1 数据库管理系统 在了解MongoDB之前需要先了解先数据库管理系统 1.1.1 什么是数据? 数据(英语:data) ...
- 转载:vs2010 问题 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
原文链接:http://www.cnblogs.com/newpanderking/articles/3372969.html >LINK : fatal error LNK1123: 转换到 ...
- 关于虚拟机中linux系统时间的问题
由于考试需求,我在vm上放置了考试用的linux环境,在进行操作的时候需要回调时间才能进行一些操作.但是每次重启之后,时间总是会和物理服务器的时间进行同步,让我非常的苦恼. 终于有一天我想清楚了如何表 ...
- struts2注解方式的验证
struts2的验证分为分编程式验证.声明式验证.注解式验证.因现在的人越来越懒,都追求零配置,所以本文介绍下注解式验证. 一.hello world 参考javaeye的这篇文章,按着做一次,起码有 ...
- 深度探索C++对象模型之第四章:函数语义学
C++有三种类型的成员函数:1.static/nonstatic/virtual 一.成员的各种调用方式 C with Classes 只支持非静态成员函数(Nonstatic Member Func ...
- Apache Tomcat下载、安装、环境变量配置以及项目部署
前言 针对在本地访问文件或资源出现的跨域问题,可以通过搭建本地服务器来解决,本篇随笔主要介绍通过搭建Apache Tomcat服务器来解决跨域.包括Apache Tomcat的下载.安装.环境变量的配 ...