项目过程中经常打日志:LOG.error("[failure][CreateOrder] param:{}", JSON.toJSONString(userCreateOrderDTO),e);
在一些日志处理过程中我们打印的日志可能是这个样的 XXX业务处理异常:{json字符串}
我们怎么获取到只包含json的字符串呢?
下面提供Java和JS两种方式:
<wiz_code_mirror>
function getJson(jsonStr) {
var stringStack = new stack();
var indexList = [];
var jsonList = [];
for (var i = 0; i < jsonStr.length; i++) {
if (jsonStr.charAt(i) == '{' || jsonStr.charAt(i) == '[') {
stringStack.push(new JsonStack(i, jsonStr.charAt(i)));
} else if (jsonStr.charAt(i) == '}' || jsonStr.charAt(i) == ']') {
if (stringStack.dataStore.length!=0) {
var js = stringStack.peek();
if (jsonStr.charAt(i) == '}' && js.char == '{') {
js = stringStack.pop();
} else if (jsonStr.charAt(i) == ']' && js.char == '[') {
js = stringStack.pop();
}
indexList.push(js.index);
indexList.push(i);
}
}
if (stringStack.dataStore.length==0 && indexList.length > 0) {
var tempStr = getJsonStr(indexList, jsonStr);
if (!(tempStr == null || tempStr.length == 0)) {
jsonList.push(tempStr);
}
indexList.splice(0,indexList.length);;
}
}
if (indexList != null && indexList.length > 0) {
var tempStr = getJsonStr(indexList, jsonStr);
if (!(tempStr == null || tempStr.length == 0)) {
jsonList.push(tempStr);
}
}
if (jsonList != null && jsonList.length > 0) {
return jsonList[0];
} else {
return null;
}
}
function getJsonStr(indexList, str) {
var temp = "";
for (var i = indexList.length - 1; i >= 0; i = i - 2) {
try {
temp = str.substring(indexList[i - 1], indexList[i] + 1);
JSON.parse(temp);
return temp;
} catch (e) {
continue;
}
}
return null;
}
function JsonStack(index, char) {
this.index = index;
this.char = char;
}
function stack() {
this.dataStore = [];//保存栈内元素,初始化为一个空数组
this.top = 0;//栈顶位置,初始化为0
this.push = push;//入栈
this.pop = pop;//出栈
this.peek = peek;//查看栈顶元素
this.clear = clear;//清空栈
this.length = length;//栈内存放元素的个数
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top - 1];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
<wiz_code_mirror>
package com.ucarinc.bizops.log;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* Created by zhangbo on 2017/12/29.
*/
public class FindJsonUtil {
public static List<String> format(String jsonStr) {
Stack<JsonStack> stringStack = new Stack<JsonStack>();
List<Integer> indexList = new LinkedList<Integer>();
List<String> jsonList = new ArrayList<String>();
for (int i = 0;i<jsonStr.length();i++) {
if(jsonStr.charAt(i)=='{'||jsonStr.charAt(i)=='['){
stringStack.push(new JsonStack(i,jsonStr.charAt(i)));
}else if(jsonStr.charAt(i)=='}'||jsonStr.charAt(i)==']'){
if(!stringStack.empty()){
JsonStack js = stringStack.peek();
if(jsonStr.charAt(i)=='}'&&js.getStr() =='{'){
js = stringStack.pop();
}else if(jsonStr.charAt(i)==']'&&js.getStr() =='['){
js = stringStack.pop();
}
indexList.add(js.getIndex());
indexList.add(i);
}
if(stringStack.empty()){
String tempStr= getJsonStr(indexList,jsonStr);
if(!(tempStr==null||tempStr.isEmpty())){
jsonList.add(tempStr);
}
indexList.clear();
}
}
}
if(indexList!=null && indexList.size()>0){
String tempStr= getJsonStr(indexList,jsonStr);
if(!(tempStr==null||tempStr.isEmpty())) {
jsonList.add(tempStr);
}
}
return jsonList;
}
private static String getJsonStr(List<Integer> indexList,String str) {
String temp= "";
for(int i = indexList.size() -1 ; i>=0 ; i=i-2){
try {
temp = str.substring(indexList.get(i - 1), indexList.get(i)+1);
JSON.parse(temp);
return str.substring(indexList.get(i - 1), indexList.get(i)+1);
}catch (Exception e){
continue;
}
}
return null;
}
static class JsonStack{
private Integer index;
private char str;
public JsonStack(Integer index, char str) {
this.index = index;
this.str = str;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public Character getStr() {
return str;
}
public void setStr(Character str) {
this.str = str;
}
}
}