Skip to main content
 首页 » 编程设计

python之如何在将字符串转换为 json 后访问 json 值

2025年05月04日24txw1958

我正在尝试转换包含如下句子的字符串列表:

my_string=u'[{"id":1275829555,"guid":11}, {"id":1275829512,"guid" :12 }]' 

并想将其转换成如下json类型:

[ 
 { 
    "id":1275829555, 
    "guid":11 
 }, 
 { 
    "id":1275829555, 
    "guid":12 
 } 
] 

不幸的是我无法访问json

print type(my_string) # <type 'unicode'> 
json_string =  str( json.dumps(my_string, ensure_ascii=False).encode('utf8') ) 
print type(json_string) # <type 'str'> 
j_obj = json.loads(json_string) 
print type(j_obj) # <type 'unicode'> 
print j_obj[0]  
[ 

如何访问 json 来处理数组中的每个条目?

请您参考如下方法:

嗨,你做的一切都是正确的,你只需要一个字符串而不是一个 unicode

import json 
import unicodedata 
 
my_string=u'[{"id":1275829555,"guid":11}, {"id":1275829512,"guid" :12 }]' 
 
my_string = unicodedata.normalize('NFKD', my_string).encode('ascii','ignore') 
print type(my_string) # <type 'unicode'> 
print my_string 
print type(json_string) # <type 'str'> 
j_obj = json.loads(str(a)) 
print type(j_obj) # <type 'unicode'> 
print j_obj[0]["id"]