Skip to main content
 首页 » 编程设计

Python之Json 转换之为什么使用相似的字符串格式会表现不同

2025年02月15日21zdz8207

我有一个关于使用 json.loads 将 python 字符串转换为字典的问题。然后使用以下函数将 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 
    } 
  } 
} 

请您参考如下方法:

    "change": +0.33 

我不知道 + 是怎么结束的,但它是无效的 JSON。再往下还有一个。

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