【问题标题】:trying to parse Json but couldn't getting output,试图解析 Json 但无法获得输出,
【发布时间】:2020-06-11 05:11:15
【问题描述】:

输入 伙计们,你们可以看到我正在尝试解析 json 但无法获得正确的输出。所以请帮我弄清楚为什么会出现这种类型的错误

import json
data='''{'name':'shashank',
phone:{'type':'int1',
'number':'+3872847239474'}
'email':{
'hide':'yes'}
}'''
info=json.loads("data")
print('name:',info["name"])
print('hide:',info["email"]["hide"])

输出

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-15-27b5f61ad286> in <module>
      6 'hide':'yes'}
      7 }'''
----> 8 info=json.loads("data")
      9 print('name:',info["name"])
     10 print('hide:',info["email"]["hide"])

~\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

    标签: json python-3.x jsonparser


    【解决方案1】:

    有几个问题。 JSON 格式不正确(您需要双引号而不是单引号,缺少一些逗号等),当您调用 json.loads 时,您传递的是 string @ 987654322@ 而不是变量data。此代码有效:

    import json
    data='''{
        "name":"shashank",
        "phone":{
            "type":"int1",
            "number":"+3872847239474"
        },
        "email":{
            "hide":"yes"
        }
    }'''
    info=json.loads(data)
    print('name:',info["name"])
    print('hide:',info["email"]["hide"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2011-03-27
      相关资源
      最近更新 更多