我收到的数据= "{\"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]
答案 0 :(得分:1)
使用JSON module
<强>实施例强>
import json
data = "{\"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}}"
print(json.loads(data))
<强>输出:强>
{u'footer': {u'ln_title': None, u'fb_feed_name': u'smartmbbs', u'fb_description': u'', u'fb_feed_caption': u'smartmbbs | VIA INQUIR.LY'}, u'tw_meta_data': {u'description': None, u'title': None, u'url': u'https://s3.amazonaws.com/campaign.ezeees.com/Ukb/a15Hh/index.html', u'image': None, u'site': u'@markethubs', u'card': u'summary_large_image'}, u'media_shorten_url': None, u'campaign_media_url': u'', u'og_meta_data': {u'site_name': u'Inquirly', u'description': None, u'title': None, u'url': u'https://s3.amazonaws.com/campaign.ezeees.com/Ukb/a15Hh/index.html', u'image': None, u'site': u'beta.inquirly.com', u'type': u'article'}, u'sms_content': u'dfbgfbfb', u'email_subject': u'dfbgfbfb', u'shorten_url': None, u'share_content': u'dfbgfbfb'}
答案 1 :(得分:-1)
将Unicode转换为utf-8格式,然后您可以执行json转储
data = "{\"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}}"
print data.decode('utf-8')
输出:
{"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}}