使用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对 ...
随机推荐
- postgres日志爆盘处理方案-转自DBA汪x
背景:我们的服务是一个带有部分批处理业务的服务,在跑历史数据入pg时会有大量日志产出,现场服务器出现过几次爆盘,询问DBA后制定了以下两个方案: 方案一:如果不关心或不会使用pg日志,通过参数控制减少 ...
- JDK8新特性之接口默认方法与静态方法
接口默认方法与静态方法 有这样一些场景,如果一个接口要添加一个方法,那所有的接口实现类都要去实现,而某些实现类根本就不需要实现这个方法也要写一个空实现,所以接口默认方法就是为了解决这个问题. 接口静态 ...
- VSCode运行JavaScript代码
方式一(推荐): 安装插件 open in window, 然后option+b
- 异步action和redux-thunk理解
异步action一般指的就是异步action创建函数 action创建函数分为同步action创建函数和异步action创建函数 同步action创建函数(最常见的): function reques ...
- scip 练习2.20
(define (same-parity x . z) (define (q? y) (= (remainder y ) )) (define (o? y) (= (remainder y ) )) ...
- 了解GTIN小记
GTIN为条形码,即"全球贸易项目代码"(Global Trade Item Number ) GTIN用作识别商品品项的全球性独一编码,是编码系统中应用最广泛的标识代码. GTI ...
- CSIC_716_20191129【 单例模式 的五种实现方式】
单例模式 单例模式:在确定类中的属性和方法不变时,需要反复调用该类的情况. 让所有通过该类实例化出的对象,都指向同一个内存地址. 优点:节省内存空间. 单例模式有五种表现形式: 1.通过class ...
- js 购物车的数量加减,对应的总价也随机变化
html相关的源码: <div class="goods_num clearfix"> <div class="num_name fl"> ...
- Ruby 安装 – Windows
Ruby 安装 – Windows 下面列出了在 Windows 机器上安装 Ruby 的步骤. 注意:在安装时,您可能有不同的可用版本. - Window 系统下,我们可以使用 RubyInstal ...
- QueryList getData()方法中多次调用来实现递归多级采集。
<?php require 'QueryList/vendor/autoload.php'; use QL\QueryList; //获取每个li里面的h3标签内容,和class为item的元素 ...