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

标签 python json

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

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"]

关于python - 如何在将字符串转换为 json 后访问 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493698/

相关文章:

jquery - 将数组发送到 jquery 中的另一个函数时出错

体式 json 数据的 JAVA 解析器 "Error:JSONObject cannot be converted to JSONArray"

json - 从ElasticSearch中的结果文档中排除字段

json - 无法解码 JSON 对象 : Expecting value: line 1 column 1 (char 0)</p>

python - 持续处理子进程中的大量 stdout 和 stderr

python - 将 python 数组打印保存到 .csv

python - 将用户导入 Firebase 时密码错误(使用 SHA256 的 password_hash)

javascript - XHR 响应中不需要的逗号出现在表格单元格中

python - Pandas 条形图 : Side By Side (No X or Y axises shared)

python - 溢出错误 : signed integer is greater than maximum