一、导入包

二、使用

package com.itheima.test;

import java.util.ArrayList;
import java.util.List; import org.junit.Test; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itheima.domain.Customer; public class Demo1 { /**
* 演示fastjson的简单的使用
*/
@Test
public void run1(){
Customer c = new Customer();
c.setCust_id(20L);
c.setCust_name("测试");
c.setCust_phone("120"); // 转换成json的字符串
String jsonString = JSON.toJSONString(c);
System.out.println(jsonString);
} @Test
public void run2(){
List<Customer> list = new ArrayList<Customer>();
Customer c = new Customer();
c.setCust_id(20L);
c.setCust_name("测试");
c.setCust_phone("120");
list.add(c); Customer c2 = new Customer();
c2.setCust_id(30L);
c2.setCust_name("测试2");
c2.setCust_phone("12000");
list.add(c2); // 转换成json的字符串
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
} /**
* 默认的情况下,fastjson禁止循环的引用
*/
@Test
public void run3(){
List<Customer> list = new ArrayList<Customer>();
Customer c = new Customer();
c.setCust_id(20L);
c.setCust_name("测试");
c.setCust_phone("120");
list.add(c);
list.add(c); // 转换成json的字符串
// String jsonString = JSON.toJSONString(list); // 禁止循环的引用
String jsonString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonString);
} /**
* 死循环的问题
*/
@Test
public void run4(){
Person p = new Person(); //person与role互相引用
p.setPname("美美");
Role r = new Role();
r.setRname("管理员");
p.setRole(r);
r.setPerson(p); // 禁止循环的引用
String jsonString = JSON.toJSONString(r,SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonString);
} }

三、对应的效果

1、

2、

3、

// 转换成json的字符串
// String jsonString = JSON.toJSONString(list);

// 禁止循环的引用
String jsonString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonString);

4、
因为3禁止循环引用产生的问题
解决:在person加注解

四、导入/crm/src/com/louis/utils/FastJsonUtil.java

package com.louis.utils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature; public class FastJsonUtil { /**
* 将对象转成json串
* @param object
* @return
*/
public static String toJSONString(Object object){
//DisableCircularReferenceDetect来禁止循环引用检测
return JSON.toJSONString(object,SerializerFeature.DisableCircularReferenceDetect);
} //输出json
public static void write_json(HttpServletResponse response,String jsonString){
response.setContentType("application/json;utf-8");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* ajax提交后回调的json字符串
* @return
*/
public static String ajaxResult(boolean success,String message)
{
Map map=new HashMap();
map.put("success", success);//是否成功
map.put("message", message);//文本消息
String json= JSON.toJSONString(map);
return json;
} /**
* JSON串自动加前缀
* @param json 原json字符串
* @param prefix 前缀
* @return 加前缀后的字符串
*/ public static String JsonFormatterAddPrefix(String json,String prefix,Map<String,Object> newmap)
{
if(newmap == null){
newmap = new HashMap();
}
Map<String,Object> map = (Map) JSON.parse(json); for(String key:map.keySet())
{
Object object=map.get(key);
if(isEntity(object)){
String jsonString = JSON.toJSONString(object);
JsonFormatterAddPrefix(jsonString,prefix+key+".",newmap); }else{
newmap.put(prefix+key, object);
} }
return JSON.toJSONString(newmap);
}
/**
* 判断某对象是不是实体
* @param object
* @return
*/
private static boolean isEntity(Object object)
{
if(object instanceof String )
{
return false;
}
if(object instanceof Integer )
{
return false;
}
if(object instanceof Long )
{
return false;
}
if(object instanceof java.math.BigDecimal )
{
return false;
}
if(object instanceof Date )
{
return false;
}
if(object instanceof java.util.Collection )
{
return false;
}
return true; }
}

8、技术分析fastJson使用的更多相关文章

  1. 蓝牙协议分析(7)_BLE连接有关的技术分析

    转自:http://www.wowotech.net/bluetooth/ble_connection.html#comments 1. 前言 了解蓝牙的人都知道,在经典蓝牙中,保持连接(Connec ...

  2. WaterfallTree(瀑布树) 详细技术分析系列

    前言 WaterfallTree(瀑布树) 是最强纯C#开源NoSQL和虚拟文件系统-STSdb专有的(版权所有/专利)算法/存储结构. 参考 关于STSdb,我之前写过几篇文章,譬如: STSdb, ...

  3. iOS直播的技术分析与实现

    HTTP Live Streaming直播(iOS直播)技术分析与实现 发布于:2014-05-28 13:30阅读数:12004 HTTP Live Streaming直播(iOS直播)技术分析与实 ...

  4. 横向技术分析C#、C++和Java优劣

    转自横向技术分析C#.C++和Java优劣 C#诞生之日起,关于C#与Java之间的论战便此起彼伏,至今不辍.抛却Microsoft与Sun之间的恩怨与口角,客观地从技术上讲,C#与Java都是对传统 ...

  5. tolua++实现lua层调用c++技术分析

    tolua++技术分析 cocos2dx+lua 前言 一直都使用 cocos2dx + lua 进行游戏开发,用 Lua 开发可以专注于游戏逻辑的实现,另外一方面可以实现热更新:而且 lua 是一个 ...

  6. 美链BEC合约漏洞技术分析

    这两天币圈链圈被美链BEC智能合约的漏洞导致代币价值几乎归零的事件刷遍朋友圈.这篇文章就来分析下BEC智能合约的漏洞 漏洞攻击交易 我们先来还原下攻击交易,这个交易可以在这个链接查询到. 我截图给大家 ...

  7. NetSarang软件中nssock2.dll模块被植入恶意代码技术分析与防护方案

    原文地址:http://blog.nsfocus.net/nssock2-dll-module-malicious-code-analysis-report/ NetSarang是一家提供安全连接解决 ...

  8. 包建强的培训课程(3):App竞品技术分析

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  9. 【渗透技术】渗透测试技术分析_TomCat

    [渗透技术]渗透测试技术分析_TomCat 本文转自:i春秋论坛 渗透测试-中间人攻击(原理)说起“中间人攻击”我想大多数对渗透测试又了解的朋友都多少有所了解,因为我们用到的次数真是非常的多.它可以将 ...

随机推荐

  1. cpp分解质因数

    原理有点像埃氏筛. #include <stdio.h> #include <iostream> #include <stdlib.h> using namespa ...

  2. 如何配置Python环境

    (1) 下载:请在Python官网下载页面(https://www.python.org/downloads/)选择合适的版本(建议选择3.5.2版)的链接,在该版本的下载页面选择合适的安装文件:64 ...

  3. (转) C#中Timer使用及解决重入(多线程同时调用callback函数)问题

    原文链接: http://www.cnblogs.com/hdkn235/archive/2014/12/27/4187925.html

  4. java mybatisGenerator with velocity

    mybatisGenerator + velocity 模板生成dao+ mapper,并将mysql命名规范的table name + column -> java命名规范的 Class na ...

  5. java后台获取URL带参demo

    URL:http://aos.wall.youmi.net/v2/check_fb_sig?order=YM130402cygr_UTb42&app=30996ced018a2a5e& ...

  6. 线程之死锁、递归锁、信号量、事件Event 、定时器

    1.死锁的现象 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相 ...

  7. FastClick

    处理移动端click事件300毫秒延迟.FastClick 是一个简单,易于使用的js库用于消除在移动浏览器上触发click事件与一个物理Tap(敲击)之间的300延迟. 1.为什么会延迟? 从点击屏 ...

  8. 数据库DRDS中间件介绍

    DRDS/TDDL alibaba. Distributed Relational Database Service. 阿里分布式数据库DRDS的前身是淘宝分布式数据库层TDDL,大概在2012年的时 ...

  9. java设计模式之责任链模式(Chain of Responsibility)

    转自:http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html 在阎宏博士的<JAVA与模式>一书中开头是这样 ...

  10. ffmpeg强制使用TCP方式读取rtsp流

    ffmpeg强制使用TCP方式处理rtsp流,参考网上资料,得知可以使用如下命令: “ffmpeg -rtsp_transport tcp -i rtsp://admin.......” 可以是使用抓 ...