gpt4 book ai didi

java - 如何从原始消息字符串中选取 json 对象?

转载 作者:行者123 更新时间:2023-12-02 00:54:22 25 4
gpt4 key购买 nike

我收到一条类似于下面所述的消息。这只是来自服务器的示例消息。原始消息可能包含许多特殊字符。如何从消息中选择准确的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com