展示型网站设计方案,wordpress.文章缩略图,惠州淡水网站建设公司,视频网站开发工具文章目录 1、GsonUtils2、String本身,可以加入日志信息3、 Json和Map转换4、Date转为Json字符串5、Json和ListT转换6、 比较两个JSON字符串的不同7、 字符串填充#xff1a;生成单据号时填充8、 String.format()的使用9、截断字符串10、splitisNotBlank 和 isNotEmpty… 文章目录 1、GsonUtils2、String本身,可以加入日志信息3、 Json和Map转换4、Date转为Json字符串5、Json和ListT转换6、 比较两个JSON字符串的不同7、 字符串填充生成单据号时填充8、 String.format()的使用9、截断字符串10、splitisNotBlank 和 isNotEmpty的区别11、判断字符串是否为Json字符串格式12、deep copy补充关于msyql的Json格式 1、GsonUtils
Json字符串转对象对象转Json字符串
UtilityClass
Slf4j
public class GsonUtils {private static final Gson GSON new Gson();public static String toJsonStr(Object object) {return GSON.toJson(object);}public static String toJsonStr(Object obj, Type type) {return GSON.toJson(obj, type);}public static T T fromJson(String json, ClassT classOfT) {return GSON.fromJson(json, classOfT);}public static T T fromJson(String json, Type typeOfT) {return GSON.fromJson(json, typeOfT);}public static String toJson(Object object) {try {return GSON.toJson(object);} catch (Exception e) {log.error(序列化失败, e);}return StringUtils.EMPTY;}
}工具类使用
String str {\status\:{\code\:0,\__isset_bit_vector\:[1]},\poiId\:1};TResponse tResponseA GsonUtils.fromJson(str, TResponse.class);2、String本身,可以加入日志信息
String.format(Duplicate asyncCreateTask request with requestId%s, billNo%s, finish it fast., request.getRequestId(), request.getNo());3、 Json和Map转换
map.put(0915, 1);
map.put(0916, 2);
String jsonStr GsonUtils.toJsonStr(map);
MapString,String json2Map GsonUtils.fromJson(jsonStr,new TypeTokenHashMapString,String(){}.getType());4、Date转为Json字符串
如果类中有Date类型的属性字段则需要在创建gson的时候设置一下日期格式Gson gson new GsonBuilder().setDateFormat(yyyy-MM-dd).create();System.out.println(gson.toJson(new Date()));5、Json和List转换
ListSkuDTO list GsonUtils.fromJson(jsonStr,new TypeTokenListSkuDTO(){}.getType());ListLong list2 GsonUtils.fromJson(Lists.newArrayList(1L,2L), new TypeTokenListLong() {}.getType());6、 比较两个JSON字符串的不同
public void t() throws JSONException {String s1 {\skuId\:1,\skuName\:\test\,\temp\:2,\address\:\bj\};String s2 {\skuId\:1,\skuName\:\test\,\temp\:3,\author\:\mjp\};// 方式一JSONCompareResult result JSONCompare.compareJSON(s1, s2, JSONCompareMode.STRICT);dependencygroupIdorg.skyscreamer/groupIdartifactIdjsonassert/artifactIdversion1.5.1/versionscopetest/scope/dependency// 1中有2中没有System.out.println(result.isMissingOnField());ListFieldComparisonFailure fieldMissing result.getFieldMissing();for (FieldComparisonFailure fieldComparisonFailure : fieldMissing) {System.out.println(fieldComparisonFailure.getExpected());//address}// 2中有1中没有System.out.println(result.isUnexpectedOnField());ListFieldComparisonFailure fieldUnexpected result.getFieldUnexpected();for (FieldComparisonFailure fieldComparisonFailure : fieldUnexpected) {System.out.println(fieldComparisonFailure.getActual());//author}// 1中2中都有但是val值不一样System.out.println(result.isFailureOnField());ListFieldComparisonFailure list result.getFieldFailures();for (FieldComparisonFailure fieldComparisonFailure : list) {System.out.println(fieldComparisonFailure.getField());//tempSystem.out.println(fieldComparisonFailure.getActual());//3System.out.println(fieldComparisonFailure.getExpected());//2}// 方式二HashMapString, Object diffMap Maps.newHashMap();Gson gson new Gson();MapString,Object json1Map gson.fromJson(s1,new TypeTokenHashMapString,Object(){}.getType());MapString,Object json2Map gson.fromJson(s2,new TypeTokenHashMapString,Object(){}.getType());for (Map.EntryString, Object entry : json1Map.entrySet()) {String k1 entry.getKey();Object v1 entry.getValue();Object v2 json2Map.get(k1);// 1中有2中没有if (v2 null) {diffMap.put(k1, v1);continue;}// 1中2中都有但是不一样if (!Objects.equals(v1, v2)){diffMap.put(k1, expect: v1 , actual: v2);}}json2Map.forEach((k2, v2) - {Object v1 json1Map.get(k2);// 2中有1中没有if (v1 null) {diffMap.put(k2, v2);}});System.out.println(diffMap);//{tempexpect:2.0, actual:3.0, addressbj, authormjp}}7、 字符串填充生成单据号时填充
Long billType 1L;
System.out.println(a StringUtils.leftPad(billType , 5, 0));// a00001
即在billType的左侧补0直到00001形成5位为止
右侧填充和两侧填充api如下
public static String leftPad(final String str, final int size, String padStr)public static String center(String str, final int size, String padStr)8、 String.format()的使用
参考https://blog.csdn.net/lonely_fireworks/article/details/7962171
简单使用
int time 3;
String nameParis String.format(达到了最大次数%s次, time);复杂使用
String contentFormat 您今天售卖的【%s】支商品有较高可能发生售罄预估销售损失pcs【%s】件预估销售损失额【%s 】元需联系补货请点击“[及时处理|%s]”查看详情。;String url sell.out.warning.link:https://xxx.xxx.xxx .com?warnType%scategoryId%sString result String.format(contentFormat, skuCount, salePcs.stripTrailingZeros().toPlainString(),saleGmv.stripTrailingZeros().toPlainString(),String.format(url, warnType, categoryId));1、将warnTpe和categoryId填充到url中成为一个完整的连接url
2、sku数pcsGMV及时处理链接url 四个字端值填充到contentFormat的%s中
9、截断字符串
String s 大家好才是真的好;
String truncate StringUtils.truncate(s, 2, 4);
System.out.println(truncate);//好才是真10、split
https://www.runoob.com/java/java-string-split.html
isNotBlank 和 isNotEmpty的区别
public static boolean isNotEmpty(String str)判断某字符串是否非空这里不能排除空格字符即StringUtils.isNotEmpty( ) true下面是示例
StringUtils.isNotEmpty(null) false
StringUtils.isNotEmpty() false
StringUtils.isNotEmpty( ) truepublic static boolean isNotBlank(String str)
判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成即StringUtils.isNotBlank( ) false下面是示例
StringUtils.isNotBlank(null) false
StringUtils.isNotBlank() false
StringUtils.isNotBlank( ) false所以在某些业务场景用isNotBlank 比 isNotEmpty要好11、判断字符串是否为Json字符串格式
try {JsonElement parse new JsonParser().parse(extra);if (parse.isJsonObject() || parse.isJsonArray()) {//YES}
} catch (JsonSyntaxException e) {//ignore
}
// NO12、deep copy // 对象deep copyMyUser myUser new MyUser();myUser.setSkuId(1L);Gson gson new Gson();MyUser newUser gson.fromJson(gson.toJson(myUser), MyUser.class);System.out.println(newUser);myUser.setSkuId(77777777777L);System.out.println(newUser);// 集合deep copyListMyUser newList JSON.parseArray(JSON.toJSONString(list), MyUser.class);//fastjsonSystem.out.println(newList);u1.setSkuId(777777777777777L);System.out.println(newList);补充关于msyql的Json格式
将json格式的内容存储mysqlmysql中字段为json而非Varchar
推荐5.7之后直接使用 字段名称 json一般都用varchar(n) 注n不要太大。太大的话考虑 JSON