开发:随笔记录之 Json字符串 与 List、HashMap对象的转换
来源:互联网 发布:高考后知哥哥去世 编辑:程序博客网 时间:2024/05/21 10:41
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
public class JsonParser {
/**
* json字符串 转成 map
*
* @param jsonStr
* @return
* @throws Exception
*/
public static HashMap<String, JsonValue> parse(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }
HashMap<String, JsonValue> retMap = null;
try {
retMap = new HashMap<String, JsonValue>();
JSONObject json = JSONObject.fromObject(jsonStr);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);
for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retMap;
}
/**
* json字符串 转成 List
*
* @param jsonStr
* @return
* @throws Exception
*/
public static List<HashMap<String, JsonValue>> parseList(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }
List<HashMap<String, JsonValue>> retList = new ArrayList<HashMap<String, JsonValue>>();
JSONArray data = JSONArray.fromObject(jsonStr);
for (int i = 0; i < data.size(); i++) {
HashMap<String, JsonValue> retMap = new HashMap<String, JsonValue>();
JSONObject json = (JSONObject) data.get(i);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);
for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
retList.add(retMap);
}
return retList;
}
/**
* HashMap<String, JsonValue> map 转成 json字符串
*
* @param jsonStr
* @return
* @throws Exception
*/
public static String parse(HashMap<String, JsonValue> map) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(retMap, jc).toString();
}
/**
* List<HashMap<String, JsonValue>> list 转成 json字符串
*
* @param jsonStr
* @return
* @throws Exception
*/
public static String parse(List<HashMap<String, JsonValue>> list) {
List<HashMap<String, Object>> tmpList = new ArrayList<HashMap<String, Object>>();
for (HashMap<String, JsonValue> map : list) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
tmpList.add(retMap);
}
JSONArray json = new JSONArray();
json.addAll(tmpList);
return json.toString();
}
/**
* 构建json
*
* @param map
* @return
*/
public static String parse(Map map) {
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(map, jc).toString();
}
对了,json的版本我用的是json-lib-2.4-jdk15.jar
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
public class JsonParser {
/**
* json字符串 转成 map
*
* @param jsonStr
* @return
* @throws Exception
*/
public static HashMap<String, JsonValue> parse(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }
HashMap<String, JsonValue> retMap = null;
try {
retMap = new HashMap<String, JsonValue>();
JSONObject json = JSONObject.fromObject(jsonStr);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);
for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retMap;
}
/**
* json字符串 转成 List
*
* @param jsonStr
* @return
* @throws Exception
*/
public static List<HashMap<String, JsonValue>> parseList(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }
List<HashMap<String, JsonValue>> retList = new ArrayList<HashMap<String, JsonValue>>();
JSONArray data = JSONArray.fromObject(jsonStr);
for (int i = 0; i < data.size(); i++) {
HashMap<String, JsonValue> retMap = new HashMap<String, JsonValue>();
JSONObject json = (JSONObject) data.get(i);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);
for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
retList.add(retMap);
}
return retList;
}
/**
* HashMap<String, JsonValue> map 转成 json字符串
*
* @param jsonStr
* @return
* @throws Exception
*/
public static String parse(HashMap<String, JsonValue> map) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(retMap, jc).toString();
}
/**
* List<HashMap<String, JsonValue>> list 转成 json字符串
*
* @param jsonStr
* @return
* @throws Exception
*/
public static String parse(List<HashMap<String, JsonValue>> list) {
List<HashMap<String, Object>> tmpList = new ArrayList<HashMap<String, Object>>();
for (HashMap<String, JsonValue> map : list) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
tmpList.add(retMap);
}
JSONArray json = new JSONArray();
json.addAll(tmpList);
return json.toString();
}
/**
* 构建json
*
* @param map
* @return
*/
public static String parse(Map map) {
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(map, jc).toString();
}
对了,json的版本我用的是json-lib-2.4-jdk15.jar
0 0
- 开发:随笔记录之 Json字符串 与 List、HashMap对象的转换
- 开发:随笔记录之 Json字符串和对象的相互转换
- json字符串与pojo对象,list集合的转换
- Json字符串与json对象的转换
- json字符串与json对象的转换
- JSON字符串对象转换List
- JSON的String字符串与Java的List列表对象的相互转换
- json对象与字符串之间的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- JSON 字符串 与 java 对象的转换
- C++ 学习步骤
- 《算法导论》笔记 第22章 总结与思考
- 网站页面屏蔽右键菜单的做法
- MySQL里面的字段类型
- Karaf jetty
- 开发:随笔记录之 Json字符串 与 List、HashMap对象的转换
- 一个简单的实现下拉框多选的插件
- hive udf debug
- Php方面的一些热门资料
- 编译VLC
- RSA算法原理(二)
- SQL Server数据库如何使用存储过程造批量数据<转>
- qt在linux与ok6410上的安装移植(2)——tslib和开发板上qt的编译
- 剪刀石头布