目录
Map、JSONObject、JSONArray转jsonObjectString
2.2 com.alibaba.fastjson.JSONObject
Map、JSONObject、JSONArray转jsonObjectString
jsonObjectString 转JSONObject(不同于net.sf.json.JSONArray)
jsonArrayString 转JSONArray(不同于net.sf.json.JSONArray)
Map转JSONObject(不同于net.sf.json.JSONArray)
一、前期说明
JSONObject本文只提com.alibaba.fastjson.JSONObject和net.sf.json.JSONObject两种。
依赖架包
com.alibaba.fastjson.JSONObject需要依赖fastjson-1.2.28.jar架包,
net.sf.json.JSONObject需要依赖commons-beanutils-1.9.3.jar、commons-collections-3.2.1.jar、
commons-lang-2.6.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar架包。
截图JSONObject相关依赖架包CSDN下载地址:http://download.csdn.net/download/justinqin/10158995
CSDN下载自动调整到50积分了,-_-||,网盘下载 https://pan.baidu.com/s/1PzgHUOCesCj4xNG6jZ666w 提取码:kpvy

二、转换实现
2.1 net.sf.json.JSONObject
Map、JSONObject、JSONArray转jsonObjectString
map.toString();
jsonObject.tonString();
jsonArray.toString();
jsonObjectString 转JSONObject
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonObjectString);
jsonArrayString 转JSONArray
net.sf.json.JSONArray jsonArray= net.sf.json.JSONArray.fromObject(jsonArrayString);
jsonObjectString 转Map
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonObjectString);
Map jsonMap= jsonObject; //JSONObject转jsonMap(自动转换)
Map转JSONObject
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(map);
JSONArray转JSONArray
//遍历获取
for (int i = 0; i < jsonArray.size(); i++) {
// 获取每一个jsonObject对象
net.sf.json.JSONObject jsonObject = jsonArray.getJSONObject(i);
}
JSONArray转JSONArray
net.sf.json.JSONArray jsonArray = new net.sf.json.JSONArray();
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
2.2 com.alibaba.fastjson.JSONObject
Map、JSONObject、JSONArray转jsonObjectString
map.toString();
jsonObject.tonString();
jsonArray.toString();
jsonObjectString 转JSONObject(不同于net.sf.json.JSONArray)
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonObjectString);
jsonArrayString 转JSONArray(不同于net.sf.json.JSONArray)
com.alibaba.fastjson.JSONArray jsonArray= com.alibaba.fastjson.JSONArray.parseArray(jsonArrayString);
jsonObjectString 转Map
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonObjectString);
Map jsonMap= jsonObject; //JSONObject转jsonMap(自动转换)
Map转JSONObject(不同于net.sf.json.JSONArray)
com.alibaba.fastjson.JSONObject jsonObject = (JSONObject) com.alibaba.fastjson.JSONObject.toJSON(map);
JSONArray转JSONArray
//遍历获取
for (int i = 0; i < jsonArray.size(); i++) {
// 获取每一个jsonObject对象
com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
}
JSONArray转JSONArray
com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray();
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
PS:
文中jsonObjectString表示JSONObject格式的字符串,如:
String jsonObjectString = "{\"name\":\"张三\",\"mobile\":\"13877908888\"}";
jsonArrayString表示JSONArray数组格式的字符串,如:
String jsonArrayString = "[{'name':'张三', 'mobile':'13877908888'},{'name':'李四', 'mobile':'13877906666'}]";
这篇博客介绍了如何在Java中使用Fastjson和json-lib库进行JSONObject和JSONArray之间的转换,包括字符串到对象,对象到字符串,以及不同对象间的互相转换。详细讲述了两个库的不同之处和转换方法。
979

被折叠的 条评论
为什么被折叠?



