原始字串:
{"code":"0","data":"NC憑證已成功創建![憑證號]為“2016-12-記賬憑證-93”,[流水標識]為0001V110000000000O82159259da6dfvouchergl0。","message":"請求成功"}
目標:取得 2016-12-記賬憑證-93。
data 欄位。[憑證號]為“ 定位起始位置。” 定位結束位置。substring 切片。String jsonStr = "...";
JSONObject obj = JSONObject.fromObject(jsonStr); // 需 json-lib 或 gson
String content = obj.getString("data");
String key = "[憑證號]為“";
int start = content.indexOf(key) + key.length();
int end = content.indexOf("”", start);
String voucherNo = content.substring(start, end);
System.out.println(voucherNo); // 2016-12-記賬憑證-93
gson、fastjson、json-lib)。\"\\【憑證號\\】為“([^”]+)” 更健壯。indexOf 回傳 -1 的異常情況。「先解析結構,再切片內容」是處理半結構化文字的標準套路。