【发布时间】:2018-04-05 13:05:40
【问题描述】:
我正在获取数据 = "{\"share_content\":\"dfbgfbfb\",\"campaign_media_url\":\"\",\"shorten_url\":null,\"media_shorten_url\":null,\"og_meta_data\":{\"site_name\":\"Inquirly\",\"description\":null,\"title\":null,\"url\":\"https://s3.amazonaws.com/campaign.ezeees.com/Ukb/a15Hh/index.html\",\"image\":null,\"site\":\"beta.inquirly.com\",\"type\":\"article\"},\"tw_meta_data\":{\"description\":null,\"title\":null,\"url\":\"https://s3.amazonaws.com/campaign.ezeees.com/Ukb/a15Hh/index.html\",\"image\":null,\"site\":\"@markethubs\",\"card\":\"summary_large_image\"},\"sms_content\":\"dfbgfbfb\",\"email_subject\":\"dfbgfbfb\",\"footer\":{\"fb_feed_name\":\"smartmbbs\",\"fb_feed_caption\":\"smartmbbs | VIA INQUIR.LY\",\"fb_description\":\"\",\"ln_title\":null}}"
当我尝试查找数据变量的类型时,我得到了<type 'unicode'>。
我尝试了使用json.loads 和json.dumps 的不同方法,但未能从数据中获取share_content 值。我遇到了不同的问题,例如字符串索引必须是整数等。
我的代码:
campaign_data = dbsession.execute("SELECT campaign_data FROM campaign_details
WHERE campaign_id = '%d'"%(218)).first()
for row in campaign_data:
sms_content = row
try:
print sms_content
print type(sms_content)
gst = json.loads(sms_content)
print type(gst)
#print json_stng['share_content']
except (ValueError, KeyError, TypeError):
print "JSON format error"
print "smssss"
我从查询中获取上述数据变量为 sms_content[0]
【问题讨论】:
-
向我们展示您尝试过的代码及其结果。看起来您要么有一个普通的 JSON 字符串,要么有一个双编码的 JSON 字符串。很难说,因为很难说你是在向我们展示 Python 文字还是原始值。
-
Unicode 与这个问题有什么关系?在任何情况下,REST API 几乎总是 Unicode,特别是 UTF8。到目前为止,几乎所有的网站都是 Unicode,例如这个网站返回 UTF8 页面。
-
@PanagiotisKanavos 它是 python 2 的内置
unicode类型,在 python 3 中变成了str。 -
@FHTMitchell,这个问题中没有 Python 代码。
-
@PanagiotisKanavos
<type 'unicode'>是unicode的 python 代表。除了 OP 所说的“unicode”之外,我什么也没说。