1,获取引号中的json字符串
- @Test
- public void test_json(){
- String input="\"normalPrice\": \"{\"storagePrice\":66,\"ud1Price\":1,\"userPeriodPrice\":99}\",";
- System.out.println(input);
- String regex=".*\"\\{([^{}]*)\\}\".*";
- String result=input.replaceAll(regex, "$1");
- System.out.println(result);
- }
2, 遇到的问题:
解决方法:
- /***
- * ("normalPrice": "{"storagePrice":66,"ud1Price":1,"userPeriodPrice":99}",)
- * to<br>
- * ("normalPrice": {"storagePrice":66,"ud1Price":1,"userPeriodPrice":99},)
- * @param input
- * @param isStrict : 是否严格,true:[^{}]<br>
- * false:.*
- * @return
- */
- public static String getJsonFromQuotes(String input,boolean isStrict){
- String regexLoose="\"(\\{.*\\})\"";
- String regexStrict="\"(\\{[^{}]*\\})\"";
- String regex=null;
- if(isStrict){
- regex=regexStrict;
- }else{
- regex=regexLoose;
- }
- String result=input.replaceAll(regex, "$1");
- return result;
- }
参数说明:
isStrict:
(a)true:严格模式,"{和}" 之间不能包含{或}
(b)false:非严格模式:"{和}"之间可以包含任意字符
应用:
- if (command.equals("unQuotesJson")) {
- String text = this.ta.getText();
- if(text!=null){
- text=RegexUtil.getJsonFromQuotes(text, false);
- this.ta.setText(text);
- }