Json是当前开发用得最多基于JavaScript语言的轻量级的数据交换格式,总结一下常用转换格式的方法,以便日后使用方便
以下为 alibabab 包各种 JSON 转换的方法总结:
1. 所需引入的第三方包:
commons-beanutils
commons-beanutils
1.9.3
commons-collections
commons-collections
3.2.1
commons-lang
commons-lang
2.6
commons-logging
commons-logging
1.2
net.sf.ezmorph
ezmorph
1.0.4
net.sf.json-lib
json-lib
2.4
jdk13
com.alibaba
fastjson
1.2.28
2. 对象User类
package com.lmx.demo;
public class User
{
private String cId;
private String uName;
private String pwd;
public User(){
}
public User(String cId, String uName, String pwd){
this.cId = cId;
this.uName = uName;
this.pwd = pwd;
}
public String getcId()
{
return cId;
}
public void setcId(String cId)
{
this.cId = cId;
}
public String getuName()
{
return uName;
}
public void setuName(String uName)
{
this.uName = uName;
}
public String getPwd()
{
return pwd;
}
public void setPwd(String pwd)
{
this.pwd = pwd;
}
}
3. Json各类转换
package com.lmx.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* alibaba 第三方包 Json 转换用法
* @author LMX
*
*/
public class Main
{
/**
* 1.通过JSON.parse把json转换为Map
*
* @param jsonStr
*/
public void getJsonToMap()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map maps = (Map)JSON.parse(jsonStr);
for (Object map : maps.entrySet())
{
System.out.println(
"key为:" + ((Map.Entry)map).getKey() + ", 值为:" + ((Map.Entry)map).getValue());
}
/*
输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 2.通过JSON.parseObject把json转换为Map
*
* @param jsonStr
* @return
*/
public void getJsonToMapByClass()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map mapTypes = JSON.parseObject(jsonStr, Map.class);
for (Object obj : mapTypes.keySet())
{
System.out.println("key为:" + obj + ", 值为:" + mapTypes.get(obj));
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 3.通过JSON.parseObject把json转换为Object
*
* @param jsonStr
* @return
*/
public void getJsonToObject()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map mapTypes = JSON.parseObject(jsonStr);
for (Object obj : mapTypes.keySet())
{
System.out.println("key为:" + obj + ", 值为:" + mapTypes.get(obj));
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 4.通过JSONObject.parse把json转换为Map
*
* @param jsonStr
*/
public void getJSONObjectToMap()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map json = (Map)JSONObject.parse(jsonStr);
for (Object map : json.entrySet())
{
System.out.println(
"key为:" + ((Map.Entry)map).getKey() + ", 值为:" + ((Map.Entry)map).getValue());
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 5.通过JSONObject.parseObject把json转换为JSONObject,并遍历单Object输出
*
* @param jsonStr
*/
public void getJSONObjectToObject()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
for (Object map : jsonObject.entrySet())
{
System.out.println(
"key为:" + ((Map.Entry)map).getKey() + ", 值为:" + ((Map.Entry)map).getValue());
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 6.通过JSONObject.parseObject把json转换为Map
*
* @param jsonStr
*/
public void getJSONObjectToMapClass()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map mapObj = JSONObject.parseObject(jsonStr, Map.class);
for (Object map : mapObj.entrySet())
{
System.out.println(
"key为:" + ((Map.Entry)map).getKey() + ", 值为:" + ((Map.Entry)map).getValue());
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
////////////// List集合和JSON相互转换工具类//////////////////////////
/**
* 7.通过JSON.toJSONString把List转换为json
*
* @param obj
* @return
*/
public static String getListToJson(List obj)
{
String jsonStr = JSON.toJSONString(obj);
return jsonStr;
// String输出: ["aa","bb","cc"]
// userModel输出: [{"cId":"100","pwd":"123456","uName":"lmx"},{"cId":"200","pwd":"123456","uName":"jqy"}]
}
/**
* 8.通过JSONArray.parseArray把json转换为List
*
* @param jsonStr
* @param model
* @return
*/
public static List getStringToList(String jsonStr, Class model)
{
List object = (List)JSONArray.parseArray(jsonStr, model);
return object;
// String输出: "aa","bb","cc"
// userModel输出: lmx,jqy
}
////////////// 对象和JSON相互转换工具类/////////////////////////
/**
* 9.通过JSONObject.toJSONString把对象转换为String
* @param model
* @return
*/
public static String getObjectToJson(T model)
{
String StrObject = JSONObject.toJSONString(model);
return StrObject;
// User输出: {"cId":"100","pwd":"123456","uName":"lmx"}
// Map输出: {"uName":"llmmxx","pwd":"123","cId":"300"}
}
/**
* 10.通过JSONObject.parseObject把String转换为对象
* @param jsonStr
* @param model
* @return
*/
public static T getStringToObject(String jsonStr, Class model)
{
return JSONObject.parseObject(jsonStr, model);
// User输出: {"cId":"100","pwd":"123456","uName":"lmx"}
// Map输出: {"uName":"llmmxx","pwd":"123","cId":"300"}
}
/**
* 11 .通过fluentPut添加一个值
* fluentPutAll fluentPut
* @param jsonStr
* @return
*/
public void setAddJSONObject()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
jsonObject.fluentPut("lll", "111");
Map der = new HashMap();
der.put("der1", "der1");
der.put("der2", "der2");
der.put("der3", "der3");
jsonObject.fluentPutAll(der);
//jsonObject.fluentClear();清除
for (Object map : jsonObject.entrySet())
{
System.out.println(
"key为:" + ((Map.Entry)map).getKey() + ", 值为:" + ((Map.Entry)map).getValue());
}
/* 输出: key为:uName, 值为:lmx
key为:pwd, 值为:123456
key为:der3, 值为:der3
key为:lll, 值为:111
key为:der1, 值为:der1
key为:cId, 值为:100
key为:der2, 值为:der2
*/
}
@SuppressWarnings("static-access")
public static void main(String[] args)
{
// TODO Auto-generated method stub
Main m = new Main();
// 1
System.out.println("getJsonToMap:");
m.getJsonToMap();
// 2
System.out.println("getJsonToMapByClass:");
m.getJsonToMapByClass();
// 3
System.out.println("getJsonToObject:");
m.getJsonToObject();
// 4
System.out.println("getJSONObjectToMap:");
m.getJSONObjectToMap();
// 5
System.out.println("getJSONObjectToObject:");
m.getJSONObjectToObject();
// 6
System.out.println("getJSONObjectToMapClass:");
m.getJSONObjectToMapClass();
// 7.1 String
System.out.println("getListToJson by String:");
List StrModel = new ArrayList();
StrModel.add("aa");
StrModel.add("bb");
StrModel.add("cc");
String strString = m.getListToJson(StrModel);
System.out.println(strString);
// 7.2 User
System.out.println("getListToJson by userModel:");
List userModel = new ArrayList();
User u = new User();
u.setcId("100");
u.setuName("lmx");
u.setPwd("123456");
User u1 = new User();
u1.setcId("200");
u1.setuName("jqy");
u1.setPwd("123456");
userModel.add(u);
userModel.add(u1);
String strUser = m.getListToJson(userModel);
System.out.println(strUser);
// 8.1 String
System.out.println("getJsonToList by String:");
List strLists = m.getStringToList(strString, String.class);
for (String strList : strLists)
{
System.out.println(strList);
}
//8.2 User对象
System.out.println("getJsonToList by User:");
List userLists = m.getStringToList(strUser,User.class);
for (User userList : userLists)
{
System.out.println(userList.getuName());
}
//9.1 User对象
System.out.println("getObjectToJson by User:");
String strUsers = m.getObjectToJson(u);
System.out.println(strUsers);
//9.2 Map
System.out.println("getObjectToJson by Map:");
Map userMap = new HashMap();
userMap.put("cId", "300");
userMap.put("uName", "llmmxx");
userMap.put("pwd", "123");
String strUsersMap = m.getObjectToJson(userMap);
System.out.println(strUsersMap);
/* //9.3 User对象
System.out.println("getObjectToJson by User:");
String strObjectUser = m.getObjectToJson(strUsers);
System.out.println(strObjectUser);
//9.4 Map对象
System.out.println("getObjectToJson by Map:");
String strObjectMap = m.getObjectToJson(strUsersMap);
System.out.println(strObjectMap);*/
//10.1 User对象
System.out.println("getStringToObject by User:");
User stringObjectUser = m.getStringToObject(strUsers,User.class);
System.out.println(JSONObject.toJSONString(stringObjectUser).toString());
//10.2 Map对象
System.out.println("getStringToObject by Map:");
Map stringObjectMap = m.getStringToObject(strUsersMap,Map.class);
System.out.println(JSONObject.toJSONString(stringObjectMap).toString());
//11
System.out.println("setAddJSONObject by User:");
m.setAddJSONObject();
}
}
总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070