Python/Json:期望用双引号括起来的属性名称 [英] Python/Json:Expecting property name enclosed in double quotes

查看:34
本文介绍了Python/Json:期望用双引号括起来的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种在 Python 中加载 JSON 对象的好方法.我发送这个json数据:

I've been trying to figure out a good way to load JSON objects in Python. I send this json data:

{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}

到后端,它将作为字符串接收,然后我使用 json.loads(data) 来解析它.

to the backend where it will be received as a string then I used json.loads(data) to parse it.

但每次我都遇到同样的异常:

But each time I got the same exception :

ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

我用谷歌搜索了它,但除了这个解决方案 json.loads(json.dumps(data)) 对我个人来说似乎没有那么有效,因为它接受任何类型的数据,即使是那些数据不是 json 格式的.

I googled it but nothing seems to work besides this solution json.loads(json.dumps(data)) which personally seems for me not that efficient since it accept any kind of data even the ones that are not in json format.

任何建议将不胜感激.

推荐答案

这个:

{
    'http://example.org/about': {
        'http://purl.org/dc/terms/title': [
            {'type': 'literal', 'value': "Anna's Homepage"}
        ]
     }
}

不是 JSON.
这:

is not JSON.
This:

{
     "http://example.org/about": {
         "http://purl.org/dc/terms/title": [
             {"type": "literal", "value": "Anna's Homepage"}
          ]
      }
}

是 JSON.


一些评论者认为以上还不够.
JSON 规范 - RFC7159 指出字符串以引号开头和结尾.那就是".
单引号 ' 在 JSON 中没有语义,只能在字符串中使用.


Some commenters suggested that the above is not enough.
JSON specification - RFC7159 states that a string begins and ends with quotation mark. That is ".
Single quoute ' has no semantic meaning in JSON and is allowed only inside a string.

这篇关于Python/Json:期望用双引号括起来的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆