Python - Json Conversion - 为什么使用类似的字符串格式表现不同

时间:2017-09-01 08:28:45

标签: python json string dictionary

我有一个关于使用json.loads将python字符串转换为dict的问题。然后使用下面的函数将dict转换为json_string:

def JsonConvert(StringInput):
    #convert string to dict
    DictTmp=json.loads(StringInput)

    #convert dict to json_string
    json_string=json.dumps(DictTmp)

    #return JSON string
    return json_string  

因此,使用上面的函数,我提供以下字符串作为示例:

stringt= ''' { "result": { "Energy": { "biggest_gainer": { "equity": 
    "Hornbeck Offshore", "change": 0.27 }, "biggest_loser": { "equity": 
    "CIRCOR Intl., Inc.", "change": -8.92 }, "change": -1.23 } } }'''        

但是只要我用字符串变量替换一些项目,Json转换就会抛出以下错误:

stringt= ''' { "result": { "Energy": { "biggest_gainer": { "equity": 
    "%s", "change": %s }, "biggest_loser": { "equity": "%s", "change": %s 
    }, "change": %s } } }'''%(Energy_gain_equity,Energy_gain_change,
    Energy_loss_equity,Energy_loss_change,sector_Energy)

给出以下错误:

JSONDecodeError                           Traceback (most recent call 
last)
<ipython-input-60-0d59da63c634> in <module>()
    152     #print(type(json_string))
    153 
--> 154 print(google_sector_report())

<ipython-input-60-0d59da63c634> in google_sector_report()
    149 
    150     string = stringer()
--> 151     json_string = JsonConvert(string)
    152     #print(type(json_string))
    153 

<ipython-input-60-0d59da63c634> in JsonConvert(StringInput)
    137     def JsonConvert(StringInput):
    138         #convert string to dict
--> 139         DictTmp=json.loads(StringInput)
    140 
    141         print(type(DictTmp))

/anaconda/lib/python3.6/json/__init__.py in loads(s, encoding, cls, 
object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, 
**kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is 
None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/anaconda/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/anaconda/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, 
err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 7 column 23 (char 146)

提前感谢您的帮助。

根据koalo的要求,在jupyter中输出完整的输出字符串:

{
  "result": {
    "Energy": {
      "biggest_gainer": {
        "equity": "McDermott International",
        "change": +0.33
      },
      "biggest_loser": {
        "equity": "Bill Barrett Corporation",
        "change": -0.43
      },
      "change": -1.48
    },
    "Basic Materials": {
      "biggest_gainer": {
        "equity": "Gold Fields Limited (ADR)",
        "change": +0.11
      },
      "biggest_loser": {
        "equity": "Jaguar Mining Inc (USA)",
        "change": -0.74
      },
      "change": -0.35
    },
    "Industrials": {
      "biggest_gainer": {
        "equity": "LML Payment Systems, Inc.",
        "change": +21.7
      },
      "biggest_loser": {
        "equity": "Chicago Bridge & Iron Co",
        "change": -1.80
      },
      "change": -0.46
    }
  }
}

1 个答案:

答案 0 :(得分:1)

    "change": +0.33

我不知道+是如何结束的,但它是无效的JSON。还有另一个下来。

当您已经了解json.dumps等等时,我不知道您为什么要使用字符串方法构建JSON。