尝试从字符串中使用json.loads会产生错误,无法解码JSON对象

时间:2016-03-10 21:11:25

标签: python json string

我正在使用jq库生成一个字符串,当我将字符串打印到控制台时,会产生一个我在帖子底部粘贴的长字符串。我将该字符串存储在out中。然后我尝试通过json.loads

将其解析为字典
json_data = json.loads(out)

这会产生以下错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我还注意到,如果我将字符串打印到文件中,它看起来很时髦:

"results":^[[1;39m[
  ^[[1;39m{
    ^[[0m^[[34;1m"name"^[[0m^[[1;39m: ^[[0m^[[0;32m"Jane Smith"^[[0m^[[1;39m,
    ^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[1;39m[
      ^[[1;39m{
        ^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[0;39m0^[[0m^[[1;39m,
        ^[[0m^[[34;1m"measurement_value"^[[0m^[[1;39m: ^[[0m^[[0;39m0.97^[[0m^[[1;39m
      ^[[1;39m}^[[0m^[[1;39m,
      ^[[1;39m{
        ^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[0;39m1^[[0m^[[1;39m,
        ^[[0m^[[34;1m"measurement_value"^[[0m^[[1;39m: ^[[0m^[[0;39m1.55^[[0m^[[1;39m
      ^[[1;39m}^[[0m^[[1;39m,
      ^[[1;39m{

所以我猜测编码会发生一些事情。我做错了什么,我该怎么做才能修复它所以我可以将这个字符串解析为JSON?

这是字符串。

{"results":[
  {
    "name": "Jane Smith",
    "day": [
      {
        "day": 0,
        "measurement_value": 0.97
      },
      {
        "day": 1,
        "measurement_value": 1.55
      },
      {
        "day": 2,
        "measurement_value": 0.67
      }
    ]
  },
  {
    "name": "Jane Smith",
    "day": [
      {
        "day": 0,
        "measurement_value": 1.25
      },
      {
        "day": 1,
        "measurement_value": 1.11
      },
      {
        "day": 2,
        "measurement_value": 0.067
      }
    ]
  },
  {
    "name": "Bob Smith",
    "day": [
      {
        "day": 0,
        "measurement_value": 0.97
      },
      {
        "day": 1,
        "measurement_value": 1.55
      },
      {
        "day": 2,
        "measurement_value": 0.67
      }
    ]
  },
  {
    "name": "Bob Smith",
    "day": [
      {
        "day": 0,
        "measurement_value": 1.25
      },
      {
        "day": 1,
        "measurement_value": 1.11
      },
      {
        "day": 2,
        "measurement_value": 0.067
      }
    ]
  }
]
}

0 个答案:

没有答案