从JSON文件加载变量到Python

问题描述:

我有一个从Google BigQuery下载的JSON,只有一个值。我如何让这个变量在我的Python脚本中表示。感谢您的帮助!从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

我在编辑中添加了我的代码。谢谢 –

+0

处理json数据是有据可查的,互联网上有无数的例子。你在问之前做过什么研究?参见[Stack Overflow用户需要多少研究工作?](http://meta.stackoverflow.com/q/261592/7432) –

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