java小知识点

目录

统计条目

LambdaQueryWrapper 

遍历并按某种条件统计

返回找到的第一个值(找主键)

map放入不同的List--Collections.singletonList

JSON.toJSONString

JSON.parseObject


统计条目

Long modelCounts = modelMapper.selectCount(modelQueryWrapper);

LambdaQueryWrapper 

LambdaQueryWrapper arLqr =
        new LambdaQueryWrapper()
            .eq(AppRelease::isDeleted, false)
            .eq(AppRelease::getAppId, appId)
            .orderByDesc(AppRelease::getCreateTime)
            .last("limit 1");
    return appReleaseMapper.selectOne(arLqr);

 

遍历并按某种条件统计

long keyCount =
 table.getFields().stream().filter(t -> Objects.equals(t.getIsKey(), true)).count();

返回找到的第一个值(找主键)

TableFieldDto tableKey =
  table.getFields().stream()
         .filter(t -> Objects.equals(t.getIsKey(), true))
         .findFirst()
         .get();

map放入不同的List--Collections.singletonList

List datasourceList = datasourceMapper.selectList(datasourceQueryWrapper);
List appInterfaceList = appInterfaceMapper.selectList(null);

Map> map = new TreeMap<>();
map.put("datasource", Collections.singletonList(datasourceList));
map.put("interface", Collections.singletonList(appInterfaceList));

equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。而equals()会判断大小写区别。

JSON.toJSONString

将对象转化为Json字符串

JSON.parseObject

将json字符串转化为相应的对象

result格式1

{
    "studentName":"true",
    "studentAge":"123" 
}
JSONObject jsonObject=JSON.parseObject(result);      //转换成object
jsonObject.getString("studentAge")    //获取object中studentAge字段;  
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));                                                     

result格式2

[
  {
     "studentName":"lily",
     "studentAge":12
  },
  {
     "studentName":"lucy",
     "studentAge":15
  }
]
JSONArray jsonArray = JSON.parseArray(result);
//JSONArray jsonArray1 = JSONArray.parseArray(result);//因为JSONArray继承了JSON,所以这样也是可以的
 
 //遍历方式1
 int size = jsonArray.size();
 for (int i = 0; i < size; i++){
    JSONObject jsonObject2 = jsonArray.getJSONObject(i);  
 System.out.println(jsonObject2.getString("studentName")+":"+jsonObject2.getInteger("studentAge"));
        }
 
 //遍历方式2
   for (Object obj : jsonArray) {
   JSONObject jsonObject2 = (JSONObject) obj;   
System.out.println(jsonObject2.getString("studentName")+":"+jsonObject2.getInteger("studentAge"));
   }

result格式3

{
    "success":"true",
    "data":{
        "shop_uid":"123"
    }
}
JSONObject shop_user =JSON.parseObject(result);  
System.out.println(JSON.parseObject(shop_user.getString("data")).getString("shop_uid"));

result格式4

{
	"success": "true",
	"data": {
		"shop_uid": "123",
		"dataChild": {
			"uidChild": "456"      //多层json嵌套
		}
	}
}
JSONObject shop_user =JSON.parseObject(result);
 
//注意:多层嵌套这里data不能先用getString("data"), 再parseObject来获取dataChild对象
JSONObject dataObject = shop_user.getJSONObject("data");
 
dataObject.getJSONObject("dataChild").getString("uidChild");

result格式5

{
 "teacherName":"crystall",
 "teacherAge":27,
 "course":
    {
       "courseName":"english",
       "code":1270
    },
  "students":
    [
      {
         "studentName":"lily",
         "studentAge":12
      },
      {
         "studentName":"lucy",
         "studentAge":15
      }
   ]
}
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
 //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
System.out.println(teacherName+","+teacherAge+","+course+","+students);

你可能感兴趣的:(Java,Web,json)