zoukankan      html  css  js  c++  java
  • 处理json中影响解析的多余引号

    在xml中,敏感字符是尖括号,在json中,敏感字符是引号,上文中我们介绍了如何处理xml中的敏感字符,本文说说如何处理json中的敏感字符。
    思路与上文相同,不再赘述。直接上代码:
    json–>javaBean

        @Test
        public void test1() {
            String json = "{"id":"1","name":"红"楼"'梦","price":"90","author":"曹雪芹"}";
            List<String> tags = new ArrayList<String>();
            Pattern pattern = Pattern.compile("\"([a-zA-Z0-9]*)\":");
            Matcher m = pattern.matcher(json);
            while (m.find()) {
                tags.add(m.group(1));
            }
            for (int i = 0; i < tags.size(); i++) {
                json = json
                        .replaceAll("\"" + tags.get(i) + "\"",
                                "^^" + tags.get(i) + "^^")
                        .replaceAll(":\"", ":^^").replaceAll("\",", "^^,");
            }
            json = json.replaceAll("\"}", "^^}").replaceAll("\"]", "^^]")
                    .replaceAll(""", "~~");
            json = json.replace("^^", """);
            ObjectMapper mapper = new ObjectMapper();
    
            try {
                Book book = mapper.readValue(json, Book.class);
                book.setName(book.getName().replace("~~", """));
                System.out.println("作者:" + book.getAuthor() + "
    书名:"
                        + book.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    json–>List

        @Test
        public void test2() {
            String json = "[{"id":"1","name":"""'梦","price":"90","author":"曹雪芹"},{"id":"2","name":"西游""","price":"45","author":"wuche"ngen"}]";
            List<String> tags = new ArrayList<String>();
            Pattern pattern = Pattern.compile("\"([a-zA-Z0-9]*)\":");
            Matcher m = pattern.matcher(json);
            while (m.find()) {
                tags.add(m.group(1));
            }
            for (int i = 0; i < tags.size(); i++) {
                json = json
                        .replaceAll("\"" + tags.get(i) + "\"",
                                "^^" + tags.get(i) + "^^")
                        .replaceAll(":\"", ":^^").replaceAll("\",", "^^,");
            }
            json = json.replaceAll("\"}", "^^}").replaceAll(""", "~~");
            json = json.replace("^^", """);
            ObjectMapper mapper = new ObjectMapper();
    
            try {
                List<Book> books = mapper.readValue(json,
                        new TypeReference<ArrayList<Book>>() {
                        });
                for (Book book : books) {
                    book.setName(book.getName().replace("~~", """));
                    book.setAuthor(book.getAuthor().replace("~~", """));
                    System.out.println("作者:" + book.getAuthor() + "
    书名:"
                            + book.getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    这两段关于json的处理基本是一致的。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    [LeetCode] 16. 3Sum Closest 解题思路
    [LeetCode] 28. Implement strStr() 解题思路
    我所理解的 KMP(Knuth–Morris–Pratt) 算法
    [LeetCode] 86. Partition List 解题思路
    [LeetCode] 61. Rotate List 解题思路
    [LeetCode] 11. Container With Most Water My Submissions Question 解题思路
    如何强制卸载阿里云盾(安骑士)监控及屏蔽云盾IP检测&附带教程
    让Nginx支持pathinfo
    linux下解压rar文件
    Linux查看物理CPU个数、核数、逻辑CPU个数
  • 原文地址:https://www.cnblogs.com/lenve/p/4614421.html
Copyright © 2011-2022 走看看