1,获取引号中的json字符串

Java代码  正则表达式实例
  1. @Test  
  2.     public void test_json(){  
  3.         String input="\"normalPrice\": \"{\"storagePrice\":66,\"ud1Price\":1,\"userPeriodPrice\":99}\",";  
  4.         System.out.println(input);  
  5.         String regex=".*\"\\{([^{}]*)\\}\".*";  
  6.         String result=input.replaceAll(regex, "$1");  
  7.         System.out.println(result);  
  8.     }  

 
正则表达式实例
 

2, 遇到的问题:
正则表达式实例
解决方法:

Java代码  正则表达式实例
  1. /*** 
  2.      * ("normalPrice": "{"storagePrice":66,"ud1Price":1,"userPeriodPrice":99}",) 
  3.      * to<br> 
  4.      * ("normalPrice": {"storagePrice":66,"ud1Price":1,"userPeriodPrice":99},) 
  5.      * @param input 
  6.      * @param isStrict : 是否严格,true:[^{}]<br> 
  7.      * false:.* 
  8.      * @return 
  9.      */  
  10.     public static String getJsonFromQuotes(String input,boolean isStrict){  
  11.         String regexLoose="\"(\\{.*\\})\"";  
  12.         String regexStrict="\"(\\{[^{}]*\\})\"";  
  13.         String regex=null;  
  14.         if(isStrict){  
  15.             regex=regexStrict;  
  16.         }else{  
  17.             regex=regexLoose;  
  18.         }  
  19.         String result=input.replaceAll(regex, "$1");  
  20.         return result;  
  21.     }  

  

参数说明:

isStrict:

(a)true:严格模式,"{和}" 之间不能包含{或}

(b)false:非严格模式:"{和}"之间可以包含任意字符

应用:

Java代码  正则表达式实例
  1. if (command.equals("unQuotesJson")) {  
  2.             String text = this.ta.getText();  
  3.             if(text!=null){  
  4.                 text=RegexUtil.getJsonFromQuotes(text, false);  
  5.                 this.ta.setText(text);  
  6.             }  

 

相关文章:

  • 2021-12-04
  • 2021-12-04
  • 2021-12-14
  • 2021-11-24
  • 2021-11-24
  • 2021-09-19
  • 2021-12-04
猜你喜欢
  • 2021-11-24
  • 2021-11-24
  • 2021-08-09
  • 2021-12-14
  • 2021-05-18
  • 2021-12-15
  • 2021-11-24
相关资源
相似解决方案