我输入了字符串,如下所示
{key:IsReprint,value:COPY}; {key:IsCancelled,value:CANCELED}
我想在输出中将以上字符串转换为如下...,想要在字符串中添加引号(键,值对)。
{"key": "IsReprint", "value":"COPY"};{"key": "IsCancelled", "value":"CANCELLED"}
请提前帮助..谢谢..
String input="{key: IsReprint, value:COPY};{key: IsCancelled,value:CANCELLED}";
if(input.contains("key:") && input.contains("value:") ){
input=input.replaceAll("key", "\"key\"");
input=input.replaceAll("value", "\"value\"");
input=input.replaceAll(":", ":\"");
input=input.replaceAll("}", "\"}");
input=input.replaceAll(",", "\",");
//System.out.println("OUTPUT----> "+input);
}
如果输入字符串如上所示,上面的代码有问题
{key:BDTV,价值:非洲有限公司|注册号:433323240833-C23441,GffBLAB |增值税号:4746660239035
等级6}
答案 0 :(得分:3)
您可以使用正则表达式来完成相同的操作,但更简洁:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsonScanner {
private final static String JSON_REGEX = "\\{key: (.*?), value:(.*?)(\\};|\\}$)";
/**
* Splits the JSON string into key/value tokens.
*
* @param json the JSON string to format
* @return the formatted JSON string
*/
private String findMatched(String json) {
Pattern p = Pattern.compile(JSON_REGEX);
Matcher m = p.matcher(json);
StringBuilder result = new StringBuilder();
while (m.find()) {
result.append("\"key\"=\"" + m.group(1) + "\", ");
result.append("\"value\"=\"" + m.group(2) + "\" ; ");
System.out.println("m.group(1)=" + m.group(1) + " ");
System.out.println("m.group(2)=" + m.group(2) + " ");
System.out.println("m.group(3)=" + m.group(3) + "\n");
}
return result.toString();
}
public static void main(String... args) {
JsonScanner jsonScanner = new JsonScanner();
String result = jsonScanner.findMatched("{key: TVREG, value:WestAfrica Ltd | VAT No: 1009034324829/{834324}<br/>Plot No.56634773,Road};{key: REGISTRATION, value:SouthAfricaLtd | VAT No: 1009034324829/{834324}<br />Plot No. 56634773, Road}");
System.out.println(result);
}
}
您可能需要调整正则表达式或输出字符串以满足您的确切要求,但这应该让您了解如何开始......
答案 1 :(得分:0)
How do I escape a string in Java?
例如:
String s =&#34; {\&#34; key \&#34;:\&#34; IsReprint \&#34;&#34 ;; //将打印为{&#34; key&#34;:&#34; IsReprint&#34;
答案 2 :(得分:0)
双引号字符必须使用Java字符串文字中的反斜杠进行转义。其他需要特殊处理的角色包括:
回车和换行:“\ r”和“\ n” 反斜杠:“\” 单引号:“\'” 水平制表符和换页符:“\ t”和“\ f”。
答案 3 :(得分:0)
这可以提供您想要的确切结果!
{
"consul": [],
"redis": [],
"postgresql": [
"primary",
"secondary"
]
}
答案 4 :(得分:0)
这是一个使用正则表达式将输入拆分为键/值对,然后使用您希望的格式聚合结果的解决方案:
// Split key value pairs
final String regexp = "\\{(.*?)\\}";
final Pattern p = Pattern.compile(regexp);
final Matcher m = p.matcher(input);
final List<String[]> keyValuePairs = new ArrayList<>();
while (m.find())
{
final String[] keyValue = input.substring(m.start() + 1, m.end() - 1) // "key: IsReprint, value:COPY"
.substring(5) // "IsReprint, value:COPY"
.split(", value:"); // ["IsReprint", "COPY"]
keyValuePairs.add(keyValue);
}
// Aggregate
final List<String> newKeyValuePairs = keyValuePairs.stream().map(keyValue ->
{
return "{\"key\": \"" + keyValue[0] + "\", \"value\":\"" + keyValue[1] + "\"}";
}).collect(Collectors.toList());
System.out.println(StringUtils.join(newKeyValuePairs.toArray(), ";"));
以下输入字符串的结果
final String input = "{key: IsReprint, value:COPY};{key: IsCancelled, value:CANCELLED};{key: BDTV, value:Africa Ltd | Reg No: 433323240833-C23441,GffBLAB | VAT No: 4746660239035<br />Level 6}";
是{"key": "IsReprint", "value":"COPY"};{"key": "IsCancelled", "value":"CANCELLED"};{"key": "BDTV", "value":"Africa Ltd | Reg No: 433323240833-C23441,GffBLAB | VAT No: 4746660239035<br />Level 6"}
答案 5 :(得分:0)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NewClass {
public static void main(String[] args) {
String input="{key: IsReprint, value:COPY};{key: IsCancelled, value:CANCELLED};{key: BDTV,value:Africa Ltd | Reg No: 433323240833-C23441,GffBLAB | VAT No: 4746660239035 Level 6}";
Matcher m1 = Pattern.compile("key:" + "(.*?)" + ",\\s*value:").matcher(input);
Matcher m2 = Pattern.compile("value:" + "(.*?)" + "}").matcher(input);
StringBuilder sb = new StringBuilder();
while(m1.find() && m2.find()){
sb.append("{\"key\": ")
.append("\"")
.append(m1.group(1).trim())
.append("\", \"value\":")
.append("\"")
.append(m2.group(1).trim())
.append("\"};");
}
String output = sb.deleteCharAt(sb.length()-1).toString();
System.out.println(output);
}
}