- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到一条类似于下面所述的消息。这只是来自服务器的示例消息。原始消息可能包含许多特殊字符。如何从消息中选择准确的 json 部分,因为我们都知道 json 部分带有大括号 {}。 Java中是否有任何json解析器可以解析原始消息格式并输出json对象。到目前为止,我已经见过很少的 json 解析器,但我们必须提供准确的 json 字符串来解析。
"hdr_Tran_Id={CARDBALANCE}~*hdr_Ref_No=1~*{res_Status=00000~}*{\"RESCARDNUMBER\":\"46877801****5006\",\"RESERRMSG\":\"成功\",\“RESTXNREFCODE\”:\“CRDHOTLIST\”,\“RESLOCALTXNDTTIME\”:\“20190121183606\”,\“RESHDRTRANID\”:\“CARDSTMTGEN\”,\“RESERRCODE\”:\“0\”,\“RESTXNREFNO\":\"12341234\",\"RESINDICATOR\":\"\"}"
我该如何选择大括号中的消息部分{}
{\"RESCARDNUMBER\":\"46877801****5006\",\"RESERRMSG\":\"SUCCESS\",\"RESTXNREFCODE\":\"CRDHOTLIST\",\"RESLOCALTXNDTTIME\":\"20190121183606\",\"RESHDRTRANID\":\"CARDSTMTGEN\",\"RESERRCODE\":\"0\",\"RESTXNREFNO\":\"12341234\", \"RESINDICATOR\":\"\"}
因为一条消息可能包含多个大括号,而少数大括号可能不包含 json 消息。
这是为观众提供的又一个原始字符串示例。
hdr_Tran_Id=LISTOFCARDS~*hdr_Ref_No=1~*res_Status=00000~*{"ResErrMsg":"SUCCESS","ResTxnRefNo":"12341234","ResMobileNumber":"9448925643","ResTxnRefCode":"LISTOFCARDS","ResErrCode":"0","ResHdrTranID":"LISTOFCARDS","ResLocalTxnDtTime":"20190121174837","ResCardNumList":[{"ResMobileNum":"9448925643","ResEmailId":"krishnakumarj@canarabank.com","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,CANCARD DIVISION,14, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"4770360117595007","ResPhoneNum":"25582496"},{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,CANCARD DIVISION,VI FLOOR, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"5298700103122003","ResPhoneNum":"25582896"},{"ResMobileNum":"9448925643","ResEmailId":"krish_jkk7@yahoo.co.in","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,NAVEEN COMPLEX,DBS WING ,###,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"6082010100018008"},{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,EDP SN CANCARD DIVN,14 M G ROAD,NAVEEN COMPLEX,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"4687780160098009","ResPhoneNum":"9448925643"}]}
最佳答案
您可以使用 String 方法 lastIndexOf 或 regexp 表达式。在下面的代码中你可以找到正则表达式的解决方案:
public static void main(String[] args) throws IOException {
String raw1 = "hdr_Tran_Id={CARDBALANCE}~*hdr_Ref_No=1~*{res_Status=00000~}*{\"RESCARDNUMBER\":\"46877801****5006\",\"RESERRMSG\":\"SUCCESS\",\"RESTXNREFCODE\":\"CRDHOTLIST\",\"RESLOCALTXNDTTIME\":\"20190121183606\",\"RESHDRTRANID\":\"CARDSTMTGEN\",\"RESERRCODE\":\"0\",\"RESTXNREFNO\":\"12341234\", \"RESINDICATOR\":\"\"}";
String raw2 = "hdr_Tran_Id=LISTOFCARDS~*hdr_Ref_No=1~*res_Status=00000~*{\"ResErrMsg\":\"SUCCESS\",\"ResTxnRefNo\":\"12341234\",\"ResMobileNumber\":\"9448925643\",\"ResTxnRefCode\":\"LISTOFCARDS\",\"ResErrCode\":\"0\",\"ResHdrTranID\":\"LISTOFCARDS\",\"ResLocalTxnDtTime\":\"20190121174837\",\"ResCardNumList\":[{\"ResMobileNum\":\"9448925643\",\"ResEmailId\":\"krishnakumarj@canarabank.com\",\"ResCustName\":\"KRISHNA KUMAR J\",\"ResCustAddr\":\"CANARA BANK,CANCARD DIVISION,14, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA\",\"ResBirthDate\":\"02-04-1968\",\"ResCardNumber\":\"4770360117595007\",\"ResPhoneNum\":\"25582496\"},{\"ResMobileNum\":\"9448925643\",\"ResCustName\":\"KRISHNA KUMAR J\",\"ResCustAddr\":\"CANARA BANK,CANCARD DIVISION,VI FLOOR, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA\",\"ResBirthDate\":\"02-04-1968\",\"ResCardNumber\":\"5298700103122003\",\"ResPhoneNum\":\"25582896\"},{\"ResMobileNum\":\"9448925643\",\"ResEmailId\":\"krish_jkk7@yahoo.co.in\",\"ResCustName\":\"KRISHNA KUMAR J\",\"ResCustAddr\":\"CANARA BANK,NAVEEN COMPLEX,DBS WING ,###,BANGALORE,INDIA\",\"ResBirthDate\":\"02-04-1968\",\"ResCardNumber\":\"6082010100018008\"},{\"ResMobileNum\":\"9448925643\",\"ResCustName\":\"KRISHNA KUMAR J\",\"ResCustAddr\":\"CANARA BANK,EDP SN CANCARD DIVN,14 M G ROAD,NAVEEN COMPLEX,BANGALORE,INDIA\",\"ResBirthDate\":\"02-04-1968\",\"ResCardNumber\":\"4687780160098009\",\"ResPhoneNum\":\"9448925643\"}]}";
Set<String> result = new HashSet<>();
result.addAll(extractJson(raw1, Pattern.compile("(\\{.*?\\})")));
result.addAll(extractJson(raw1, Pattern.compile("(\\{.*\\[.*?\\]\\})")));
result.addAll(extractJson(raw2, Pattern.compile("(\\{.*?\\})")));
result.addAll(extractJson(raw2, Pattern.compile("(\\{.*\\[.*?\\]\\})")));
result.forEach(System.out::println);
}
private static Set<String> extractJson(String raw, Pattern p) throws IOException {
Set<String> jsons = new HashSet<>();
Matcher m = p.matcher(raw);
while (m.find()) {
String groupJson = m.group(1);
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> typeRef
= new TypeReference<HashMap<String, Object>>() {
};
try {
Map<String, Object> map = mapper.readValue(groupJson, typeRef);
jsons.add(groupJson);
} catch (JsonParseException e) {
//ignore
}
}
return jsons;
}
输出:
{"RESCARDNUMBER":"46877801****5006","RESERRMSG":"SUCCESS","RESTXNREFCODE":"CRDHOTLIST","RESLOCALTXNDTTIME":"20190121183606","RESHDRTRANID":"CARDSTMTGEN","RESERRCODE":"0","RESTXNREFNO":"12341234", "RESINDICATOR":""}
{"ResErrMsg":"SUCCESS","ResTxnRefNo":"12341234","ResMobileNumber":"9448925643","ResTxnRefCode":"LISTOFCARDS","ResErrCode":"0","ResHdrTranID":"LISTOFCARDS","ResLocalTxnDtTime":"20190121174837","ResCardNumList":[{"ResMobileNum":"9448925643","ResEmailId":"krishnakumarj@canarabank.com","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,CANCARD DIVISION,14, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"4770360117595007","ResPhoneNum":"25582496"},{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,CANCARD DIVISION,VI FLOOR, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"5298700103122003","ResPhoneNum":"25582896"},{"ResMobileNum":"9448925643","ResEmailId":"krish_jkk7@yahoo.co.in","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,NAVEEN COMPLEX,DBS WING ,###,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"6082010100018008"},{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,EDP SN CANCARD DIVN,14 M G ROAD,NAVEEN COMPLEX,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"4687780160098009","ResPhoneNum":"9448925643"}]}
{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,CANCARD DIVISION,VI FLOOR, NAVEEN COMPLEX,M G ROAD,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"5298700103122003","ResPhoneNum":"25582896"}
{"ResMobileNum":"9448925643","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,EDP SN CANCARD DIVN,14 M G ROAD,NAVEEN COMPLEX,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"4687780160098009","ResPhoneNum":"9448925643"}
{"ResMobileNum":"9448925643","ResEmailId":"krish_jkk7@yahoo.co.in","ResCustName":"KRISHNA KUMAR J","ResCustAddr":"CANARA BANK,NAVEEN COMPLEX,DBS WING ,###,BANGALORE,INDIA","ResBirthDate":"02-04-1968","ResCardNumber":"6082010100018008"}
关于java - 如何从原始消息字符串中选取 json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57832393/
在下面的代码中,我想不必添加undefined作为filteredDevice的类型注解。我认为一个被过滤的设备不应该是未定义的,因为我过滤掉了未定义的设备。 但是如果我删除 undefined类型注
我有一个 UIButton,其文本来自服务器。按钮的宽度是固定的。我想要的是,如果按钮的文本超过按钮的宽度,文本应该在按钮内选取框。如果文本适合按钮宽度,则不应有选取框效果。 我点击了一些链接(one
我正在从头开始实现 webgl 选取,并决定走 GLSL 路线,而不是光线相交测试。 所以我将整个场景渲染到一个单独的帧缓冲区中,为每个对象分配一个唯一的颜色,该颜色作为统一变量传递给片段着色器。当场
例如,我有包含多个对象的数组, var arr = ["a", "b", "c", "d"]; 我想要的是从该数组中选取 2 个随机对象,例如“a”和“c”,并将这 2 个对象推送到另一个数组中,例如
我是一名优秀的程序员,十分优秀!