@Test
public void test1() {
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
String jsonStr = JSON.toJSONString(customer);
System.out.println(jsonStr);
}
/**
* 将 List 集合转换为 JSON 字符串
*/
@Test
public void test2() {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
Customer customer2 = new Customer();
customer2.setId(1);
customer2.setCustName("Bob");
customer2.setAddress("ShangHai");
lists.add(customer2);
String jsonStr = JSON.toJSONString(lists);
System.out.println(jsonStr);
}
@Test
public void test3() {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
lists.add(customer);
String jsonStr = JSON.toJSONString(lists);
System.out.println(jsonStr);
}
@Test
public void test3() {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
lists.add(customer);
String jsonStr = JSON.toJSONString(lists, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonStr);
}
@Test
public void test4() {
Manager mgr = new Manager();
mgr.setMgrId(1);
mgr.setMgrName("Tom");
Department dept = new Department();
dept.setDeptId(2);
dept.setDeptName("DEV");
mgr.setDept(dept);
dept.setManager(mgr);
String jsonStr = JSON.toJSONString(dept, SerializerFeature.DisableCircularReferenceDetect);
// String jsonStr = JSON.toJSONString(mgr, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonStr);
}
public class Department {
private Integer deptId;
private String deptName;
@JSONField(serialize=false)
private Manager manager;
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
}
package qi.ssh.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) {
// TODO Auto-generated catch block
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;
}
}
package com.software.jackson;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Customer {
private int id;
private String name;
public Customer(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity(){
return "BeiJing";
}
@JsonIgnore
public String getSchool(){
return "School";
}
public static void main(String[] args) throws JsonProcessingException {
//创建ObjectMapper对象
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer(1, "Tom");
List<Customer> lists = Arrays.asList(customer, new Customer(2, "Bob"));
//调用 ObjectMapper 的 writeValueAsString(xxx) 方法,把一个对象或几个传入,转为一个 JSON 字符串
String jsonStr = mapper.writeValueAsString(lists);
System.out.println(jsonStr);
}
}
@Test
public void test2() throws JsonProcessingException {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
lists.add(customer);
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(lists);
System.out.println(jsonStr);
}
@Test
public void test1() throws JsonProcessingException {
Manager mgr = new Manager();
mgr.setMgrId(1);
mgr.setMgrName("Tom");
Department dept = new Department();
dept.setDeptId(2);
dept.setDeptName("DEV");
mgr.setDept(dept);
dept.setManager(mgr);
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(dept);
System.out.println(jsonStr);
}
@Test
public void test2() {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
Customer customer2 = new Customer();
customer2.setId(1);
customer2.setCustName("Bob");
customer2.setAddress("ShangHai");
lists.add(customer2);
Gson gson = new Gson();
String jsonStr = gson.toJson(lists);
System.out.println(jsonStr);
}
@Test
public void test3() {
List<Customer> lists = new ArrayList<>();
Customer customer = new Customer();
customer.setId(1);
customer.setCustName("Tom");
customer.setAddress("BeiJing");
lists.add(customer);
lists.add(customer);
Gson gson = new Gson();
String jsonStr = gson.toJson(lists);
System.out.println(jsonStr);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有