将变量从JSON文件加载到Python

时间:2017-07-17 03:40:59

标签: python json

我有一个从Google BigQuery下载的JSON,只有一个值。如何在我的Python脚本中表示此变量。谢谢你的帮助!

import smtplib
import os

from slacker import Slacker

def post_slack():
    """Post slack message."""
    try:
        slack = Slacker(token)

        obj = slack.chat.post_message(
            channel='#dataworksapp',
            as_user=True,
            attachments=[
        {
            "fallback": "Required plain-text summary of the attachment.",
            "color": "#36a64f",
            "pretext": "Optional text that appears above the attachment block",
            "author_name": "Data Works",
            "author_link": "http://flickr.com/bobby/",
            "author_icon": "http://flickr.com/icons/bobby.jpg",
            "title": "BlueBox Weekly Information",
            "title_link": "https://console.cloud.google.com/storage/browser/firebase_results/?project=dataworks-356fa",
            "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "BlueBox Devices Connected",
                    "value": "High",
                    "short": 'true'
                },
                {
                    "title": "BlueBox Connection Time",
                    "value": "High",
                    "short": 'true'
                }
            ],
            "image_url": "http://my-website.com/path/to/image.jpg",
            "thumb_url": "http://example.com/path/to/thumb.png",
            "footer": "Slack API",
            "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
            "ts": 123456789
        }])
        print obj.successful, obj.__dict__['body']['channel'], obj.__dict__[
            'body']['ts']
    except KeyError, ex:
        print 'Environment variable %s not set.' % str(ex)

if __name__ == '__main__':
    post_slack()

这是我的代码。我需要"值"附件中的一部分是从JSON上传的变量。

1 个答案:

答案 0 :(得分:1)

import json
json_text = '{"some":"json"}'
some_dict = json.loads(json_text)