📣 独立日限时特惠: VIP通行证立减$68
12
返回列表 发新帖
楼主: pinkdatura
跳转到指定楼层
上一主题 下一主题
收起左侧

求教一道算法题, parse json

全局:
楼主看这样行不

  1.     public static String pretty(String s) {
  2.         StringBuilder sb = new StringBuilder();
  3.         int indent = 0;
  4.         char pre = 0;
  5.         for (char c : s.toCharArray()) {
  6.             if (Character.isWhitespace(c)) continue;
  7.             if (c == ']' || c == '}') indent--;
  8.             if (pre == '[' || pre == '{' || pre == ',' || c == ']' || c == '}') {
  9.                 sb.append('\n');
  10.                 for (int i = 0; i < indent; i++) sb.append("  ");
  11.             }      
  12.             sb.append(c);
  13.             if (c == '[' || c == '{') indent++;
  14.             pre = c;
  15.         }      
  16.         return sb.toString();
  17.     }
复制代码
回复

使用道具 举报

🔗
张欣 2017-9-13 08:49:48 | 只看该作者
全局:
"tag": [      1,      "home",      2    ], 这个不用换行?
回复

使用道具 举报

全局:
  1. public static ArrayList<String> prettyJSON(String a) {
  2.         Stack<Indent> st = new Stack<Indent>();

  3.         int tabCount = 0;

  4.         ArrayList<String> ans = new ArrayList<String>();

  5.         for (int i = 0; i < a.length(); i++) {

  6.             if (a.charAt(i) == '{' || a.charAt(i) == '[') {
  7.                 ans.add(get(tabCount).append(a.charAt(i)).toString());
  8.                 tabCount++;
  9.             } else if (a.charAt(i) == '}' || a.charAt(i) == ']') {
  10.                 tabCount--;
  11.                 ans.add(get(tabCount).append(a.charAt(i)).toString());
  12.             } else if (a.charAt(i) == ' ') {
  13.             } else if (a.charAt(i) == ',') {
  14.                 ans.set(ans.size() - 1, ans.get(ans.size() - 1).concat(","));
  15.             } else {
  16.                 int start = i;
  17.                 while (i < a.length() && (a.charAt(i) != ',' && a.charAt(i) != '[' && a.charAt(i) != '{' && a.charAt(i) != ']' && a.charAt(i) != '}')) {
  18.                     i++;
  19.                 }

  20.                 if (i >= a.length()) {
  21.                     break;
  22.                 }

  23.                 if (a.charAt(i) == ',') {
  24.                     ans.add(get(tabCount).toString() + a.substring(start, i + 1));
  25.                 } else if (a.charAt(i) == '}' || a.charAt(i) == ']') {
  26.                     ans.add(get(tabCount).toString() + a.substring(start, i));
  27.                     i--;
  28.                 } else if (a.charAt(i) == '{' || a.charAt(i) == '[') {
  29.                     ans.add(get(tabCount).toString() + a.substring(start, i));
  30.                     i--;
  31.                 }
  32.             }
  33.         }

  34.         return ans;
  35.     }

  36.     private static StringBuilder get(int tabCount) {
  37.         StringBuilder toAdd = new StringBuilder();
  38.         for (int j = 0; j < tabCount; j++) {
  39.             toAdd.append('\t');
  40.         }
  41.         return toAdd;
  42.     }

  43.     class Indent {
  44.         char c;
  45.         int tab;

  46.         Indent(char c, int tab) {
  47.             this.c = c;
  48.             this.tab = tab;
  49.         }
  50.     }

  51.     public static void main(String[] args) {
  52.         String s = "{\"id\": \"0002\",\"type\": \"donut\",\"name\": \"Cake\",\"ppu\": 0.55, \"batters\":{\"batter\":[{ \"id\": \"1001\", \"type\": \"Regular\" },{ \"id\": \"1002\", \"type\": \"Chocolate\" }]},\"topping\":[{ \"id\": \"5001\", \"type\": \"None\" },{ \"id\": \"5002\", \"type\": \"Glazed\" }]}";
  53.         List<String> res = Solution.prettyJSON(s);
  54.         for (String str : res) {
  55.             System.out.println(str);
  56.         }
  57.     }
复制代码

评分

参与人数 1大米 +3 收起 理由
不知道小帅 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

全局:
我想要offer真的 发表于 2020-12-15 04:26
[mw_shl_code=java,true]public static ArrayList prettyJSON(String a) {
        Stack st = new Stack( ...

这个测了一下可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表