使用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对 ...
随机推荐
- java IO 类概述表
列举常用的类方便查看,温故知新! byte input byte output character input character output Basic InputStream OutputStr ...
- 序列化之 TLV
目录 序列化之 TLV 1. 紧凑模式 2. 可扩展性 3. 更好的可扩展性 4. 自解释性 5. 跨语言特性 6. 代码自动化:IDL语言 7. 总结 序列化之 TLV 通信协议可以理解两个节点之间 ...
- (数据科学学习手札61)xpath进阶用法
一.简介 xpath作为对网页.对xml文件进行定位的工具,速度快,语法简洁明了,在网络爬虫解析内容的过程中起到很大的作用,除了xpath的基础用法之外(可参考我之前写的(数据科学学习手札50)基于P ...
- API Gateway和Route 53及CloudFront的连携使用
API Gateway部署出来之后的url网址对于普通用户并不友好,所以肯定是需要一个正常的域名来作为url进行访问. 主要有以下几点, API Gateway可以自定义域名 自定义的域名要从Rout ...
- python 编写暴力破解mysql用户名密码
本文摘自别人的,自己运行调试了一下#!/user/bin/env python#-*- coding:utf-8 -*- import pymysql#导入连接数据库的模块import sys cla ...
- vue-cli 4058错误
vue搭建项目 执行命令 npm install -g vue-cli 出现错误 4058 1.按照文章http://www.jb51.net/article/126221.htm操作没有效果 2 ...
- 使用babel转码器,让浏览器支持es6语法
ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,可是很多浏览器并不支持es6语法,所以我们需要一个转码工具, 把es6的语法转换成浏览器支持的javascr ...
- 5432. 【NOIP2017提高A组集训10.28】三元组
题目 题目大意 给你\(X+Y+Z\)个三元组\((x_i,y_i,z_i)\). 然后选\(X\)个\(x_i\),选\(Y\)个\(y_i\),选\(Z\)个\(z_i\). 每个三元组只能选择其 ...
- Ruby 环境变量
Ruby 环境变量 Ruby 解释器使用下列环境变量来控制它的行为.ENV 对象包含了所有当前设置的环境变量列表. 变量 描述 DLN_LIBRARY_PATH 动态加载模块搜索的路径. HOME 当 ...
- Android客户端转换php服务端获取的时间戳的转换
今天在用JSON获取后台的数据的时候,发现一个奇怪的现象就是返回来的时间戳都是1970年这样的,很是纠结,最后发现时php和Java中时间的格式不一样造成的,所以我们本地客户端要做一个转换: /** ...