使用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对 ...
随机推荐
- opencv bwlabel
int bwLabel(const Mat& imgBw, Mat& imgLabeled) { Mat imgClone = Mat(imgBw.rows + , imgBw.col ...
- linux内核编译时如何根据spec指定编译包
问题: 1> rpmbuild -bb SPECS/kernel.spec --define="_topdir `pwd`" 编译 出的包并未包含kernel-firmwar ...
- 冲上云霄,Dubbo Go!
来源:开源中国社区 5 月 21 日,经过一年多的孵化,Apache Dubbo 从 Apache 软件基金会毕业,成为 Apache 顶级项目.推荐:厉害了,Dubbo 正式毕业! Dubbo 是阿 ...
- 洛谷 P2652 同花顺(离散化)
洛谷 P2652 同花顺(题面) 手动模拟了一下,其实离散化排序可以起很大作用题目要求花色相同,数字连续,那么我们要做的就是找一种花色,并提取出其中一串数字留下那些舍弃的牌换成相应花色,并和之前留下的 ...
- 如果通过cookies和localStorage取值?
1. getCook : 设定setCookie值 let setCookie = setCookie('wan',data,7); function setCookie(c_name,value,e ...
- CentOS 7 用 yum 安装 Nginx
在 CentOS 7 中,直接使用 yum 安装 Nignx 会提示无下载源.因此,需要添加 Nginx 的下载源到 yum: sudo rpm -Uvh http://nginx.org/packa ...
- C预处理之宏定义
#include <stdio.h> //定义不带参数的宏 #define PI 3.14 /*********************************************** ...
- tomcat nio apr
NIO[root@localhost ~]# vim /usr/local/tomcat9/conf/server.xml<Connector port="8080" pro ...
- [HDU3333]Turing Tree
莫队模板题... 不过树状数组也可以做...跟HH的项链几乎一模一样,离线询问,然后记录前缀,更新的时候把前缀删掉就好了,然而这题开long long,卡空间 //hgs AK IOI,IMO,ICH ...
- 格式化抽象本地地址(实战linux socket编程)
格式化抽象本地地址传统AF_UNIX套接口名字的麻烦之一就在于总是调用文件系统对象.这不是必须的,而且也不方便.如果原始的文件系统对象并没有删除,而在bind调用时使用相同的文件名,名字赋值就会失败. ...