欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python將字符串轉換成json的方法小結

 更新時間:2019年07月09日 09:12:42   作者:legehappy  
這篇文章主要介紹了python將字符串轉換成json的方法小結,通過實例代碼給大家介紹將字符串型的數(shù)據(jù)轉換成dict類型遇到的問題,需要的朋友可以參考下

最近在工作中遇到了一個小問題,如果要將字符串型的數(shù)據(jù)轉換成dict類型,我第一時間就想到了使用json函數(shù)。但是里面出現(xiàn)了一些問題

1、通過json來轉換:

In [1]: import json
In [2]: mes = '{"InsId": 2, "name": "lege-happy", "CreationTime": "2019-04-23T03:18:02Z"}'
In [3]: mes_to_dict = json.loads(mes)
In [4]: print type(mes_to_dict)
<type 'dict'>

以上的方式轉換是沒問題的,但是加入mes的格式為mes = '{"InsId": 2, "name": "lege-happy", "CreationTime": "2019-04-23T03:18:02Z"}'的時候使用json來轉換的時候又會發(fā)生什么呢?

In [5]: import json

In [6]: mes = "{'InsId': 1, 'name': 'lege-error', 'CreationTime': '2019-04-24T03:18:02Z'}"

In [7]: mes_to_dict = json.loads(mes)
---------------------------------------------------------------------------
ValueError                Traceback (most recent call last)
<ipython-input-7-77264851f35b> in <module>()
----> 1 mes_to_dict = json.loads(mes)

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
  336       parse_int is None and parse_float is None and
  337       parse_constant is None and object_pairs_hook is None and not kw):
--> 338     return _default_decoder.decode(s)
  339   if cls is None:
  340     cls = JSONDecoder

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
  364
  365     """
--> 366     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  367     end = _w(s, end).end()
  368     if end != len(s):

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
  380     """
  381     try:
--> 382       obj, end = self.scan_once(s, idx)
  383     except StopIteration:
  384       raise ValueError("No JSON object could be decoded")

ValueError: Expecting property name: line 1 column 2 (char 1)

所以使用 json 進行轉換存在一個潛在的限制:

由于 json 語法規(guī)定 數(shù)組或對象之中的字符串必須使用雙引號,不能使用單引號 (官網(wǎng)上有一段描述是 “A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes” ),因此上面的轉換是錯誤的:

通過eval來轉換:

In [8]: mes = '{"InsId": 2, "name": "lege-happy", "CreationTime": "2019-04-23T03:18:02Z"}'

In [9]: mes_dict = eval(mes)

In [10]: print type(mes_dict)
<type 'dict'>

In [11]:

In [11]: mes = mes = "{'InsId': 1, 'name': 'lege-error', 'CreationTime': '2019-04-24T03:18:02Z'}"

In [12]: mes_dict = eval(mes)

In [13]: print type(mes_dict)
<type 'dict'>

上面的例子可以看出進行轉換的時候不存在使用json轉換的問題,但是我們需要注意的是使用eval會存在安全問題,比如:

串型的輸入直接計算。比如,她會將'1+1'的計算串直接計算出結果。

In [14]: value = eval(raw_input('please input a value string:'))
please input a value string:2 + 2

In [15]: value
Out[15]: 4

從上面來看,eval功能可謂非常強大,即可以做string與list,tuple,dict之間的類型轉換,還可以做計算器使用!更有甚者,可以對她能解析的字符串都做處理,而不顧忌可能帶來的后果!所以說eval強大的背后,是巨大的安全隱患?。?!

比如說用戶惡意輸入下面的字符串:

open(r'D://filename.txt', 'r').read()
__import__('os').system('dir')
__import__('os').system('rm -rf /etc/*')

那么eval就會顯示你電腦目錄結構,讀取文件,刪除文件等等。如果是格盤等更嚴重的操作,她也會照做不誤?。。?顯然這個不符合我們的需求!
通過literal_eval轉換:

In [20]: import ast
In [21]: mes = '{"InsId": 2, "name": "lege-happy", "CreationTime": "2019-04-23T03:18:02Z"}'
In [22]: mes_dict = ast.literal_eval(mes)
In [23]: print type(mes_dict)
<type 'dict'>
In [24]:
In [24]:
In [24]: mes = mes = "{'InsId': 1, 'name': 'lege-error', 'CreationTime': '2019-04-24T03:18:02Z'}"
In [25]: mes_dict = ast.literal_eval(mes)
In [26]: print type(mes_dict)
<type 'dict'>

使用 ast.literal_eval 進行轉換既不存在使用 json 進行轉換的問題,也不存在使用 eval 進行轉換的 安全性問題,因此推薦使用 ast.literal_eval。

我們來看看官方文檔怎么描述literal_eval的:

def literal_eval(node_or_string):
  """
  Safely evaluate an expression node or a string containing a Python
  expression. The string or node provided may only consist of the following
  Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
  and None.
  """

意思說ast模塊就是幫助Python應用來處理抽象的語法解析的。而該模塊下的literal_eval()函數(shù):則會判斷需要計算的內(nèi)容計算后是不是合法的python類型,如果是則進行運算,否則就不進行運算。

比如說上面的計算操作,及危險操作,如果換成了ast.literal_eval(),都會拒絕執(zhí)行。

所以個人推薦大家轉換dict的時候,出于安全考慮對字符串進行類型轉換的時候,最好使用ast.literal_eval()函數(shù)!

總結

以上所述是小編給大家介紹的python將字符串轉換成json的方法小結 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

最新評論