JAVA刪除字符串固定下標(biāo)字串的實(shí)現(xiàn)
此解決方式在企業(yè)中有所應(yīng)用,適合Java初級(jí)開發(fā)學(xué)習(xí),參考。
需要修改的報(bào)文
當(dāng)你拿到的報(bào)文是這樣的
{
"input": {
"sdfsn": "23u4209350-2",
"fsfs": "128412094",
"sgsgsg": "15821059",
"inssgsuplc_admdfdfdvs": "125125332",
"dgh": "125215312",
"dfgdfg": "215215",
"sdhdsh": "",
"sdfsn": "",
"shdfshdshdsh": "shsdh",
"sdhdsh": "shsh.0",
"shsdhsd": "1",
"shsdh": "1607",
"input": {
"data": {
"dhfsdhsd": "235325",
"shsdhsdh": "03",
"dgd": "BE0445360",
"dfhfdh": "11",
"dshshsd": 76.56,
"ghjrfgj": "01",
"grjf": "234623626",
"hjfd": "236436",
"djfdfgjdfj": "45634",
"exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
"dfjgdfj": "",
"dfjdfjgdfj": "56745745",
"dfgjdfgjh": 45756758,
"jdfgjhfdgj": 0,
}
}
},
"output": {
"output": {
"r757": {
"dhfsdhsd": "235325",
"shsdhsdh": "03",
"dgd": "BE0445360",
"dfhfdh": "11",
"dshshsd": 76.56,
"ghjrfgj": "01",
"grjf": "234623626",
"hjfd": "236436",
"djfdfgjdfj": "45634",
"exp_content": "",
"dfjgdfj": "",
"dfjdfjgdfj": "56745745",
"dfgjdfgjh": 45756758,
"jdfgjhfdgj": 0,
},
"sdfgsdfg": [
{
"sgasgag": "4673476",
"agasgdas": 5675467,
"asgasgasg": "",
"asdgasgas": 4567456754,
"dhsdsxchsdh": 54675467,
"sdfhsdhsdh": "5674756457"
}
]
},
"erherth": 0,
}
}
這一看就知道上邊的報(bào)文在postman里邊肯定會(huì)報(bào)錯(cuò),因?yàn)閑xp_Content,因此他又沒有用到,所以你想把他刪掉。其實(shí)也沒那么難刪
也就是用到了流轉(zhuǎn)字符串。字符串固定字符查找,然后進(jìn)行字符串轉(zhuǎn)字符流,刪掉字符流中固定字符,之后再轉(zhuǎn)回來。因?yàn)樽址呀?jīng)是final了所以很多用法都是使用字符串轉(zhuǎn)字符流實(shí)現(xiàn)的
實(shí)現(xiàn)代碼如下
private JSONObject resolveApplicationJson(HttpServletRequest request) {
InputStream is = null;
String json = null;
try {
is = request.getInputStream();
json = IOUtils.toString(is, "UTF-8");
json=json.replaceAll("\\r|\n|\t","");
int index= json.indexOf("exp_Content");
int indexfirst=json.indexOf("{", index);
int indexlast=json.indexOf("}",index);
if (index!=-1 && indexlast !=-1 &&indexfirst !=-1) {
StringBuffer stringBuffer = new StringBuffer(json);
stringBuffer.delete(indexfirst,indexlast+1);
json=stringBuffer.toString();
}
} catch (IOException e) {
throw new RuntimeException("CANNOT get reader from request!", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
return new JSONObject(json);
} catch (JSONException e) {
throw new RuntimeException("CANOT CONVET JSON:[" + json + "] to JSONObject!", e);
}
}
多存在多個(gè)不符合規(guī)定的數(shù)據(jù)然后你要?jiǎng)h掉怎么操作呢?
這也很簡(jiǎn)單,如果你看了這一篇文章,你要更好的解決方式,期待一起探討,學(xué)習(xí)進(jìn)步(^-^)V
當(dāng)你拿到的報(bào)文是這樣的。
{
"input": {
"sdfsn": "23u4209350-2",
"fsfs": "128412094",
"sgsgsg": "15821059",
"inssgsuplc_admdfdfdvs": "125125332",
"dgh": "125215312",
"dfgdfg": "215215",
"sdhdsh": "",
"sdfsn": "",
"shdfshdshdsh": "shsdh",
"sdhdsh": "shsh.0",
"shsdhsd": "1",
"shsdh": "1607",
"input": {
"data": {
"dhfsdhsd": "235325",
"shsdhsdh": "03",
"dgd": "BE0445360",
"dfhfdh": "11",
"dshshsd": 76.56,
"ghjrfgj": "01",
"grjf": "234623626",
"hjfd": "236436",
"djfdfgjdfj": "45634",
"exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
"dfjgdfj": "",
"dfjdfjgdfj": "56745745",
"dfgjdfgjh": 45756758,
"jdfgjhfdgj": 0,
}
}
},
"output": {
"output": {
"r757": {
"dhfsdhsd": "235325",
"shsdhsdh": "03",
"dgd": "BE0445360",
"exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
"dfhfdh": "11",
"dshshsd": 76.56,
"ghjrfgj": "01",
"grjf": "234623626",
"hjfd": "236436",
"djfdfgjdfj": "45634",
"exp_content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
"dfjgdfj": "",
"dfjdfjgdfj": "56745745",
"dfgjdfgjh": 45756758,
"jdfgjhfdgj": 0,
},
"sdfgsdfg": [
{
"sgasgag": "4673476",
"agasgdas": 5675467,
"asgasgasg": "",
"asdgasgas": 4567456754,
"dhsdsxchsdh": 54675467,
"sdfhsdhsdh": "5674756457"
}
]
},
"erherth": 0,
}
}解決方式如下:根據(jù)需要寫出正則表達(dá)式,再使用正則看看有幾個(gè)這樣的不需要的符號(hào),然后循環(huán)遍歷幾次,之后找到這部分,最后刪除掉。
實(shí)現(xiàn)代碼如下
# 全局變量
private static String REGEX = "exp_[c,C]ontent";
# 方式方法
Pattern p = Pattern.compile(REGEX);
// 獲取 matcher 對(duì)象
Matcher m = p.matcher(JSON);
List<Integer> list = new ArrayList();
while(m.find()){
list.add(m.start());
}
int index=0;
int indexfirst=0;
int indexlast=0;
for (int j = 0; j < list.size(); j++) {
index=JSON.indexOf("exp_",indexlast);
indexfirst=JSON.indexOf("{", index);
indexlast=JSON.indexOf("}",index);
StringBuffer stringBuffer =null;
if (indexlast !=-1 &&indexfirst !=-1) {
stringBuffer = new StringBuffer(JSON);
stringBuffer.delete(indexfirst,indexlast+1);
}
JSON=stringBuffer.toString();
}到此這篇關(guān)于JAVA刪除字符串固定下標(biāo)字串的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JAVA刪除下標(biāo)字串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 進(jìn)制轉(zhuǎn)換實(shí)例詳解
這篇文章主要介紹了java 進(jìn)制轉(zhuǎn)換實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java中的遞增i++與++i的實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java中的i++與++i的實(shí)現(xiàn)原理詳解,在Java中,i++是一種常見的遞增操作符,用于將變量i的值增加1,它是一種簡(jiǎn)潔且方便的方式來實(shí)現(xiàn)循環(huán)和計(jì)數(shù)功能,i++可以用于各種情況,本文來看一下其實(shí)現(xiàn)原理,需要的朋友可以參考下2023-10-10
Double.parseDouble()與Double.valueOf()的區(qū)別及說明
Jackson忽略字段實(shí)現(xiàn)對(duì)字段進(jìn)行序列化和反序列化
基于servlet的執(zhí)行原理與生命周期(全面解析)
Spring中@Validated和@Valid區(qū)別淺析

