Json字符串转对象和转List集合操作(alibabab版本)Json字符串转对象和转List集合操作(alibabab版本)
Json是当前开发⽤得最多基于JavaScript语⾔的轻量级的数据交换格式,总结⼀下常⽤转换格式的⽅法,以便⽇后使⽤⽅便
以下为 alibabab 包各种 JSON 转换的⽅法总结:
1. 所需引⼊的第三⽅包:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>ph</groupId>object to
<artifactId>ezmorph</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk13</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
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.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 : Set())
{
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 + ", 值为:" + (obj));
}
/* 输出: key为:address, 值为:sz
key为:Id, 值为:1
key为:userName, 值为:lmx
key为:pwd, 值为:123456
*/
}
/**
* 3.通过JSON.parseObject把json转换为Object
* @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 + ", 值为:" + (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 : Set())
{
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 : Set())
{
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
*/
public void getJSONObjectToMapClass()
{
String jsonStr = "{\"userName\":\"lmx\",\"pwd\":\"123456\",\"address\":\"sz\",\"Id\":\"1\"}";
Map mapObj = JSONObject.parseObject(jsonStr, Map.class);
for (Object map : Set())
{
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.通过JSONString把List<T>转换为json
*
* @param obj
* @return
*/
public static <T> String getListToJson(List<T> obj)
{
String jsonStr = JSONString(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<T>
*
* @param jsonStr
* @param model
* @return
*/
public static <T> List<T> getStringToList(String jsonStr, Class<T> model)
{
List<T> object = (List<T>)JSONArray.parseArray(jsonStr, model);
return object;
// String输出: "aa","bb","cc"
// userModel输出: lmx,jqy
}
/
/ 对象和JSON相互转换⼯具类/
/**
* 9.通过JSONString把对象转换为String
* @param model
* @return
*/
public static <T> String getObjectToJson(T model)
{
String StrObject = JSONString(model);
return StrObject;
// User输出: {"cId":"100","pwd":"123456","uName":"lmx"}
/
/ Map输出: {"uName":"llmmxx","pwd":"123","cId":"300"}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论