Java 截取字符串双引号里的字符

 Java截取字符串双引号里的字符输出

public static void main(String[] args) {
        String str = "{\"product\":\"1\",\"code\":\"5772\",\"param\":\"200\"}";
        Pattern p1= Pattern.compile("\"(.*?)\"");
        Matcher m = p1.matcher(str);
        List<String> params = new ArrayList<>();
        while (m.find()) {
            System.out.println((m.group().trim().replace("\"","")));
            params.add((m.group().trim().replace("\"","")));
        }

        Map map = new HashMap();
        for (int i = 0; i < params.size()/2; i++){
            map.put(params.get(2*i),params.get(2*i+1));
        }
        System.out.println(map);
    }

输出结果 

product
1
code
5772
param
200
{product=1, code=5772, param=200}
发布了45 篇原创文章 · 获赞 51 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/qq_40162735/article/details/103454316