gpt4 book ai didi

javascript - 字符集有什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:56 28 4
gpt4 key购买 nike

尝试使用 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 Controller 中)它是:

%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 解析异常。那么如何编码呢?​​

最佳答案

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

关于javascript - 字符集有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com