String a="{'code':100,'data':{'grdbl':100.0,'bxl':646,'fwl':0,'mytsl':0}}";
//先把String对象转换成json对象
JSONObject object=JSONObject.fromObject(a);
System.out.println(object.get("code"));
System.out.println(object.getJSONObject("data"));
System.out.println(object.getJSONObject("data").get("grdbl"));
System.out.println(object.getJSONObject("data").get("bxl"));
///////////////////////////////////上面获取json中数据,下面是jsonArray中数据
/*[
{
'otitle':'会',
'source':'7'
},
{
'otitle':'不会',
'source':'3'
}
]
取出jsonArray中的值*/
String str ="[{'otitle':'会','source':'7'},{'otitle':'不会','source':'3'}]";
//1.把字符串类型的json数组对象转化JSONArray
JSONArray jsonArray=JSONArray.fromObject(str);
//2、循环遍历这个数组
for(int i=0;i
重组json格式(JS写法)
let arr=[
{
"title":"值班",
"name": "勤务工作",
"number":2,
"type": "待建设"
},
{
"title":"值班",
"name": "值班时间",
"number":2,
"type": "待建设"
},
{
"title":"备勤",
"name": "静态备勤",
"number":2,
"type": "已建设"
},
{
"title":"备勤",
"name": "动态备勤",
"number":2,
"type": "待建设"
}
];
//大括号{}创建一个对象 []数组
var map = {},dest = [];
for(var i = 0; i < arr.length; i++){
var ai = arr[i];
if(!map[ai.title]){
dest.push({
title: ai.title,
data: [ai]
});
map[ai.title] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.title == ai.title){
dj.data.push(ai);
break;
}
}
}
}
console.log(JSON.stringify(dest));
返回格式如下
[{
"title": "值班",
"data": [{
"title": "值班",
"name": "勤务工作",
"number": 2,
"type": "待建设"
},
{
"title": "值班",
"name": "值班时间",
"number": 2,
"type": "待建设"
}
]
},
{
"title": "备勤",
"data": [{
"title": "备勤",
"name": "静态备勤",
"number": 2,
"type": "已建设"
},
{
"title": "备勤",
"name": "动态备勤",
"number": 2,
"type": "待建设"
}
]
}
]
使用java分类并且排序json
package org.example;
import com.alibaba.fastjson.JSON;
import java.util.*;
public class GroupJSON {
public static void main(String[] args) {
String jsonList = "[{\"model_flag\":\"1\",\"mxmc\":\"周三接待\",\"title\":\"唯一的A\",\"count(1)\":\"4\"},{\"model_flag\":\"1\",\"mxmc\":\"小区巡防\",\"title\":\"唯一的A\",\"count(1)\":\"3\"},{\"model_flag\":\"1\",\"mxmc\":\"纠纷调解\",\"title\":\"唯二的B\",\"count(1)\":\"3\"},{\"model_flag\":\"1\",\"mxmc\":\"境外人员\",\"title\":\"唯二的B\",\"count(1)\":\"3\"}]";
//要格式化 不然出现下面状况等于号=情况
System.out.println(JSON.toJSONString(sortGroupByFiled(jsonList,"title","model_flag")));
List
分组排序后的json
[{
"title": "唯一的A",
"list": [{
"model_flag": "1",
"mxmc": "周三接待",
"count(1)": 4,
"title": "唯一的A"
}, {
"model_flag": "1",
"mxmc": "小区巡防",
"count(1)": 3,
"title": "唯一的A"
}]
}, {
"title": "唯二的B",
"list": [{
"model_flag": "1",
"mxmc": "纠纷调解",
"count(1)": 3,
"title": "唯二的B"
}, {
"model_flag": "1",
"mxmc": "境外人员",
"count(1)": 3,
"title": "唯二的B"
}]
}]
再分享个JSONUtil
package org.example.utils;
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 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 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
pom为
com.xiaoleilu
hutool-all
3.0.1