5.插曲:POST过来的数据是十六进制的情况。

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"}

通过校验

110706_Pd1U_2971347.png

圆满了,我的jerrymouse。接下来就一马平川了。

 

转载于:https://my.oschina.net/marjeylee/blog/783614

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值