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的处理基本是一致的。

  • 相关阅读:
    OO第二单元——多线程(电梯)
    OO前三次作业思考(第一次OO——Blog)
    P2016 战略游戏——树形DP大水题
    P1108 低价购买——最长下降子序列+方案数
    P1041 传染病控制——暴力遍历所有相同深度的节点
    P2502 [HAOI2006]旅行——暴力和并查集的完美结合
    2019.10.25字符串——zr
    P3719 [AHOI2017初中组]rexp——递归模拟
    树状数组优化最长上升子序列
    P1378 油滴扩展——搜索小记
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5083040.html
Copyright © 2011-2022 走看看