简体   繁体   中英

converting unicode to json data in python

I am getting 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}}" "{\\"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}}"

When I try to find type of the data variable I am getting <type 'unicode'> . I have tried different ways using json.loads and json.dumps but failed to get share_content value from the data. I am getting different issues like string indices must be integers, etc.

My code :

 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"

I am getting above data variable from query as sms_content[0]

Use the JSON module

Ex:

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))

Output:

{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'}

Convert Unicode into utf-8 format then you can do json dumps

        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')

Output:

    {"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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM