源代码在下面:(JSON使用了阿里的fastJSON)
目录
JSON数组分组
JSON数组字段复制
源代码
JSON数组分组
方法 |
groupBy(String jsonStr, String byKey, String byName) |
jsonStr |
json字符串(数组形势) |
byKey |
分组的主要依据字段 |
byName |
分组次要字段(对字段的解释,例如:按照字典目录分组,bykey为字典目录编码,byName为字典目录名称) |
方法 |
groupBy(String jsonStr, String byKey) |
jsonStr |
json字符串(数组形势) |
byKey |
分组的主要依据字段 |
方法 |
groupBy(JSONArray befortArray, String byKey) |
befortArray |
json数组 |
byKey |
分组的主要依据字段 |
实例:
String jsonarry = "[
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]";
JSONUtils.groupBy(jsonarry , "id", "name");
//结果
[
{
"id": "1001",
"name": "值1",
"data": [
{"id": "1001", "name": "值1", "value": "111"},
{ "id": "1001", "name": "值1", "value": "11111"}
]
},
{
"id": "1002",
"name": "值2",
"data": [
{ "id": "1002", "name": "值2", "value": "25462" },
{ "id": "1002", "name": "值2", "value": "23131"},
{"id": "1002", "name": "值2","value": "2315432" }
]
},
{
"id": "1003",
"name": "值3",
"data": [
{"id": "1003", "name": "值3", "value": "333333" }
]
}
]
JSON数组字段复制
方法 |
fieldsConversio(String jsonStr, Map<String, String> fromTo |
jsonStr |
json字符串(数组形势) |
fromTo |
key json数组中对象的字段名 ;value :将key复制,并修改名字 |
实例:
String jsonarry = "[
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"}
]";
Map<String, String> conversioMap = new HashMap<String, String>();
conversioMap.put("name", "name1");
conversioMap.put("id", "id1");
JSONUtils.fieldsConversio(jsonarry ,conversioMap);
//结果
[
{"id":"1001","id1":"1001","name":"值1","name1":"值1","value":"111"},
{"id":"1001","id1":"1001","name":"值1","name1":"值1","value":"11111"}
]
源代码
package cn.com.kgo.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.util.ObjectUtil;
public class JSONUtils {
/**
* json数组的一个字段,复制为另一个名字
*
* @param jsonStr
* @param fromTo
* :key json数组中对象的字段名 ;value :将key复制,并修改名字
* @return
* @author lizhenming
* @date 2019年1月14日
*/
public static JSONArray fieldsConversio(String jsonStr, Map<String, String> fromTo) {
if (ObjectUtil.isNull(fromTo)) {
return JSON.parseArray(jsonStr);
}
JSONArray _resulearr = JSON.parseArray(jsonStr);
for (Iterator iterator = _resulearr.iterator(); iterator.hasNext();) {
JSONObject _oJsonObject = (JSONObject) iterator.next();
for (Map.Entry<String, String> entry : fromTo.entrySet()) {
String _from = entry.getKey();
String _to = entry.getValue();
if (ObjectUtil.isNotNull(_oJsonObject.get(_from))) {
_oJsonObject.put(_to, _oJsonObject.get(_from));
}
}
}
return _resulearr;
}
/**
* JSON字符串分组
*
* @param jsonStr
* @param byKey
* @param byName
* @return
* @author lizhenming
* @date 2019年1月14日
*/
public static JSONArray groupBy(String jsonStr, String byKey, String byName) {
if (StringUtils.isBlank(jsonStr)) {
return null;
}
Map<Object, String> map = new HashMap<Object, String>();
JSONArray _resuleArray = new JSONArray();
JSONArray _befortArray = JSON.parseArray(jsonStr);
for (Iterator iterator = _befortArray.iterator(); iterator.hasNext();) {
JSONObject _item = (JSONObject) iterator.next();
// 如果JSON分组不存在
if (StringUtils.isBlank(map.get(_item.get(byKey)))) {
JSONObject _group_obj = new JSONObject();
JSONArray _group_list = new JSONArray();
_group_obj.put(byKey, _item.get(byKey));
_group_obj.put(byName, _item.get(byName));
_group_list.add(_item);
_group_obj.put("data", _group_list);
map.put(_item.get(byKey), "exist");
_resuleArray.add(_group_obj);
} else {
for (int _i = 0; _i < _resuleArray.size(); _i++) {
JSONObject _temp_obj = _resuleArray.getJSONObject(_i);
if (_temp_obj.get(byKey).equals(_item.get(byKey))) {
_temp_obj.getJSONArray("data").add(_item);
}
}
}
}
return _resuleArray;
}
/**
* JSON分组
*
* @param jsonStr
* @param byKey
* @return
* @author lizhenming
* @date 2019年1月14日
*/
public static JSONArray groupBy(String jsonStr, String byKey) {
if (StringUtils.isBlank(jsonStr)) {
return null;
}
Map<Object, String> map = new HashMap<Object, String>();
JSONArray _resuleArray = new JSONArray();
JSONArray _befortArray = JSON.parseArray(jsonStr);
for (Iterator iterator = _befortArray.iterator(); iterator.hasNext();) {
JSONObject _item = (JSONObject) iterator.next();
// 如果JSON分组不存在
if (StringUtils.isBlank(map.get(_item.get(byKey)))) {
JSONObject _group_obj = new JSONObject();
JSONArray _group_list = new JSONArray();
_group_obj.put(byKey, _item.get(byKey));
_group_list.add(_item);
_group_obj.put("data", _group_list);
map.put(_item.get(byKey), "exist");
_resuleArray.add(_group_obj);
} else {
for (int _i = 0; _i < _resuleArray.size(); _i++) {
JSONObject _temp_obj = _resuleArray.getJSONObject(_i);
if (_temp_obj.get(byKey).equals(_item.get(byKey))) {
_temp_obj.getJSONArray("data").add(_item);
}
}
}
}
return _resuleArray;
}
/**
* JSON分组
*
* @param befortArray
* @param byKey
* @return
* @author lizhenming
* @date 2019年1月14日
*/
public static JSONArray groupBy(JSONArray befortArray, String byKey) {
if (ObjectUtil.isNull(befortArray)) {
return null;
}
Map<Object, String> map = new HashMap<Object, String>();
JSONArray _resuleArray = new JSONArray();
for (Iterator iterator = befortArray.iterator(); iterator.hasNext();) {
JSONObject _item = (JSONObject) iterator.next();
// 如果JSON分组不存在
if (StringUtils.isBlank(map.get(_item.get(byKey)))) {
JSONObject _group_obj = new JSONObject();
JSONArray _group_list = new JSONArray();
_group_obj.put(byKey, _item.get(byKey));
_group_list.add(_item);
_group_obj.put("data", _group_list);
map.put(_item.get(byKey), "exist");
_resuleArray.add(_group_obj);
} else {
for (int _i = 0; _i < _resuleArray.size(); _i++) {
JSONObject _temp_obj = _resuleArray.getJSONObject(_i);
if (_temp_obj.get(byKey).equals(_item.get(byKey))) {
_temp_obj.getJSONArray("data").add(_item);
}
}
}
}
return _resuleArray;
}
}