【问题标题】:Whats wrong with char set?字符集有什么问题?
【发布时间】:2016-09-06 07:06:35
【问题描述】:

尝试使用 JSON 格式将请求数据从 JavaScript 转换为 Java。 JavaScript 中的请求正文如下所示:

{
    "id": "3",
    "name": "Chicken pasta",
    "description": "Lets make chicken pasta",
    "category": "Unassigned",
    "favorite": true,
    "prepTime": "",
    "cookTime": "",
    "ingredients": [
        {}
    ],
    "steps": [],
    "user": {
        "id": "2",
        "username": "user2"
    }
}

但在服务器端(在我的 Java 控制器中)是:

%7B%0A%09%22id%22%3A+%223%22%2C%0A%09%22name%22%3A+%22Chicken+pasta%22%2C%0A%09%22description%22%3A+%22Lets+make+chicken+pasta%22%2C%0A%09%22category%22%3A+%22Unassigned%22%2C%0A%09%22favorite%22%3A+true%2C%0A%09%22prepTime%22%3A+%22%22%2C%0A%09%22cookTime%22%3A+%22%22%2C%0A%09%22ingredients%22%3A+%5B%0A%09%09%7B%7D%0A%09%5D%2C%0A%09%22steps%22%3A+%5B%5D%2C%0A%09%22user%22%3A+%7B%0A%09%09%22id%22%3A+%222%22%2C%0A%09%09%22username%22%3A+%22user2%22%0A%09%7D%0A%7D=

所以我得到 JSON 解析异常。 那么如何编码呢?​​

【问题讨论】:

  • 它的编码你必须解码它
  • 使用 JSON.stringify() 方法将 javascript 对象转换为 json 字符串
  • 已经用过了,你在JS端看到的字符串是JSON.stringify(object)
  • 建议研究如何在 java 中使用 mime 类型“application/x-www-form-urlencoded”解码请求数据 - 似乎更像是一个 java 问题。
  • 您在服务器上收到的字符串是 URL 编码的。您需要先对其进行 URL 解码,然后才能将其解析为 JSON。有关 java 中的 URL 解码,请参阅 this SO question

标签: javascript java json character-encoding


【解决方案1】:

您在服务器上收到的字符串是 URL 编码的。您需要先对其进行 URL 解码,然后才能将其解析为 JSON。请参阅this SO question 关于 java 中的 URL 解码。

【讨论】:

    【解决方案2】:

    是的,Thomas,你说得对,我已经发现了。但是没有使用 URLDecoder.decode,只是自己写了几串代码:)

       public static String toDecimal (String hexStr) {
            char[] hex = hexStr.toCharArray();
            char current;
            String result = "";
            for (int i=0;i<hex.length;i++) {
                if (hex[i] == '%') {
                    current = (char) ((toDecimal(hex[i+1]) * 16) + toDecimal(hex[i+2]));
                    i+=2;
                } else {
                    if (hex[i] == '+') {
                        current = ' ';
                    } else {
                        current = hex[i];
                    }
                }
                result += current;
            }
            while (!(result.endsWith("}") || result.endsWith("]"))) {
                result = result.substring(0, result.length()-1);
            }
            return result;
        }
    
        private static int toDecimal (char hex) {
            switch (hex) {
                case '0': return 0;
                case '1': return 1;
                case '2': return 2;
                case '3': return 3;
                case '4': return 4;
                case '5': return 5;
                case '6': return 6;
                case '7': return 7;
                case '8': return 8;
                case '9': return 9;
                case 'A': return 10;
                case 'B': return 11;
                case 'C': return 12;
                case 'D': return 13;
                case 'E': return 14;
                case 'F': return 15;
            }
            return -1;
        }
    

    与前面提到的 URLDecoder.decode 方法的结果相同,但我的函数也会从结尾删除所有字符,而找不到 Json 的结尾('}' 或']'),我不知道为什么,但我得到了字符串from JS 以 '=' 符号结尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2011-07-02
      • 2014-07-15
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多