name=11%E9%BB%98%E9%BB%98%E5%A4%A7&age=152&children%5Bname%5D=%E5%84%BF%E5%AD%90&children%5Bage%5D=1&childr en%5Bson%5D%5Bname%5D=%E5%AD%99%E5%AD%90&children%5Bson%5D%5Bage%5D=1
这串东西目标树转为JSON字符串,可以被解析成字符串。
1首先要做的是获取所有十六进制%。
现在大体思路:遍历字节数组,当不是%的时候,把字节传入要组装的字符串。
当碰到%的时候,新建字符串,把后两位加到字符串后,再判断后三位是否是%若是。接着组装。否则结束。用while循环来做把。
定义一份方法。
哇塞,成功了。
public static void main(String[] args) throws UnsupportedEncodingException { StringBuffer firstStr = new StringBuffer(); String as = "name=11%E9%BB%98%E9%BB%98%E5%A4%A7&age=152&children%5Bname%5D=%E5%84%BF%E5%AD%90&children%5Bage%5D=1&children%5Bson%5D%5Bname%5D=%E5%AD%99%E5%AD%90&children%5Bson%5D%5Bage%5D=1"; char[] chars = as.toCharArray(); int charLenth = chars.length; for (int i = 0; i < charLenth; ) { if (chars[i] != '%') { firstStr.append(chars[i]); i++; } else { StringBuffer tempStr = new StringBuffer(); boolean isAdd = true; while (isAdd) { i = addCh(tempStr, chars, i); if (chars[i] != '%') { isAdd = false; } } String s = tempStr.toString(); byte[] baKeyword = new byte[s.length() / 2]; for (int j = 0; j < baKeyword.length; j++) { try { baKeyword[j] = (byte) (0xff & Integer.parseInt( s.substring(j * 2, j * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8"); firstStr.append(s); } catch (Exception e1) { e1.printStackTrace(); } } } System.out.println(firstStr); } //这个方法要做的事情就是传入 字符串,char数组,和&的位置。把&后两位加到字符串当中。 //返回的是i新指向 &的三位 private static int addCh(StringBuffer tempStr, char[] chars, int i) { i++; tempStr.append(chars[i]); i++; tempStr.append(chars[i]); i++; return i; }
出来的结果是这个样子的。
name=11默默大&age=152&children[name]=儿子&children[age]=1&children[son][name]=孙子&children[son][age]=1
折腾了我二分之一天。上着班搞这些是不是不太好。。我靠终于出来了。贴一下最新代码。
public static void main(String[] args) throws UnsupportedEncodingException { String as = "name=11%E9%BB%98%E9%BB%98%E5%A4%A7&age=152&children%5Bname%5D=%E5%84%BF%E5%AD%90&children%5Bage%5D=1&children%5Bson%5D%5Bname%5D=%E5%AD%99%E5%AD%90&children%5Bson%5D%5Bage%5D=1"; as = as.replace("&", "\",\""); as = as.replace("=", "\":\""); char[] chars = as.toCharArray(); int charLenth = chars.length; StringBuffer firstStr = new StringBuffer(); firstStr.append("{\""); for (int i = 0; i < charLenth; ) { if (chars[i] != '%') { firstStr.append(chars[i]); i++; } else { StringBuffer tempStr = new StringBuffer(); boolean isAdd = true; while (isAdd) { i = addCh(tempStr, chars, i); if (chars[i] != '%') { isAdd = false; } } String s = tempStr.toString(); byte[] baKeyword = new byte[s.length() / 2]; for (int j = 0; j < baKeyword.length; j++) { try { baKeyword[j] = (byte) (0xff & Integer.parseInt( s.substring(j * 2, j * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8"); firstStr.append(s); } catch (Exception e1) { e1.printStackTrace(); } } } firstStr.append("\"}"); System.out.println(firstStr); } //这个方法要做的事情就是传入 字符串,char数组,和&的位置。把&后两位加到字符串当中。 //返回的是i新指向 &的三位 private static int addCh(StringBuffer tempStr, char[] chars, int i) { i++; tempStr.append(chars[i]); i++; tempStr.append(chars[i]); i++; return i; }
运行结果:{"name":"11默默大","age":"152","children[name]":"儿子","children[age]":"1","children[son][name]":"孙子","children[son][age]":"1"}
通过校验
圆满了,我的jerrymouse。接下来就一马平川了。