使用 json 模块解码 json 字符串时出现问题

发布于 2024-12-04 04:14:46 字数 1434 浏览 2 评论 0原文

联系服务器后,我得到以下字符串作为响应

{"kind": "t2", "data": {"has_mail": null, "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 301, "is_gold": false, "is_mod": false, "id": "425zf", "has_mod_mail": null}}

,该字符串在我的脚本中存储为 type 'str'

现在,当我尝试使用 json.dumps(mystring, sort_keys=True, indent=4) 对其进行解码时,我得到了这个。

"{\"kind\": \"t2\", \"data\": {\"has_mail\": null, \"name\": \"shadyabhi\", \"created\": 1273919273.0, \"created_utc\": 1273919273.0, \"link_karma\": 1343, \"comment_karma\": 301, \"is_gold\": false, \"is_mod\": false, \"id\": \"425zf\", \"has_mod_mail\": null}}"

应该是这样的

shadyabhi@archlinux ~ $ echo '{"kind": "t2", "data": {"has_mail": "null", "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 299, "is_gold": "false", "is_mod": "false", "id": "425zf", "has_mod_mail": "null"}}' | python2 -mjson.tool
{
    "data": {
        "comment_karma": 299, 
        "created": 1273919273.0, 
        "created_utc": 1273919273.0, 
        "has_mail": "null", 
        "has_mod_mail": "null", 
        "id": "425zf", 
        "is_gold": "false", 
        "is_mod": "false", 
        "link_karma": 1343, 
        "name": "shadyabhi"
    }, 
    "kind": "t2"
}
shadyabhi@archlinux ~ $

那么,到底出了什么问题呢?

After contacting a server I get the following strings as response

{"kind": "t2", "data": {"has_mail": null, "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 301, "is_gold": false, "is_mod": false, "id": "425zf", "has_mod_mail": null}}

which is stored as type 'str' in my script.

Now, when I try to decode it using json.dumps(mystring, sort_keys=True, indent=4), I get this.

"{\"kind\": \"t2\", \"data\": {\"has_mail\": null, \"name\": \"shadyabhi\", \"created\": 1273919273.0, \"created_utc\": 1273919273.0, \"link_karma\": 1343, \"comment_karma\": 301, \"is_gold\": false, \"is_mod\": false, \"id\": \"425zf\", \"has_mod_mail\": null}}"

which should really be like this

shadyabhi@archlinux ~ $ echo '{"kind": "t2", "data": {"has_mail": "null", "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 299, "is_gold": "false", "is_mod": "false", "id": "425zf", "has_mod_mail": "null"}}' | python2 -mjson.tool
{
    "data": {
        "comment_karma": 299, 
        "created": 1273919273.0, 
        "created_utc": 1273919273.0, 
        "has_mail": "null", 
        "has_mod_mail": "null", 
        "id": "425zf", 
        "is_gold": "false", 
        "is_mod": "false", 
        "link_karma": 1343, 
        "name": "shadyabhi"
    }, 
    "kind": "t2"
}
shadyabhi@archlinux ~ $

So, what is it that's going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妳是的陽光 2024-12-11 04:14:46

您需要先加载它,然后才能转储它。试试这个:

data = json.loads(returnFromWebService)

json.dumps(data, sort_keys=True, indent=4)

要添加更多细节 - 您正在接收一个字符串,然后要求 json 库将其转储为字符串。这没有多大意义。您首先需要做的是将数据放入更有意义的容器中。通过调用 loads,您可以获取返回的字符串值并将其解析为实际的 Python 字典。然后,您可以将该数据传递到 dumps,它会使用您请求的格式输出字符串。

You need to load it before you can dump it. Try this:

data = json.loads(returnFromWebService)

json.dumps(data, sort_keys=True, indent=4)

To add a bit more detail - you're receiving a string, and then asking the json library to dump it to a string. That doesn't make a great deal of sense. What you need to do first is put the data into a more meaningful container. By calling loads you take the string value of the return and parse it into an actual Python Dictionary. Then, you can pass that data to dumps which outputs a string using your requested formatting.

双手揣兜 2024-12-11 04:14:46

你把事情搞倒了。如果要将字符串转换为数据结构,则需要使用 json.loads(thestring)。 json.dumps() 用于将数据结构转换为 json 编码字符串。

You have things backwards. If you want to convert a string to a data structure you need to use json.loads(thestring). json.dumps() is for converting a data structure to a json encoded string.

银河中√捞星星 2024-12-11 04:14:46

您应该转储一个对象(如字典),然后该对象变成一个字符串,而不是相反......请参阅此处

请改用 json.loads() 。

You are supposed to dump an object (like a dictionary) which then becomes a string, not the other way round... see here.

Use json.loads() instead.

糖果控 2024-12-11 04:14:46

您需要 json.loadsdumps 方法用于另一种方式(将对象转储为 json 字符串)。

You want json.loads. The dumps method is for going the other way (dumping an object to a json string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文