从JSON转储响应Python中提取单个值

时间:2016-11-07 18:23:53

标签: json python-2.7

我在从JSON转储中提取单个值时遇到了一些困难。我试图从使用GoogleFinance包生成的JSON输出中提取股票的单个值,但我不断收到此错误消息:

  

预期的字符串或缓冲区

我试图在论坛上找到解决方案,但似乎没有任何效果。

我尝试过的一件事是使用json.loads将JSON加载到字符串中,但我一直在同一个墙上运行。

    from googlefinance import getQuotes
    import json
    import sys

    Apple = json.dumps(getQuotes('AAP'), indent=2) #gets the quote from Apple stock. I know I should use the json.loads but that doesn't seem to be working for the getQuotes. 


#JSON output    
    [
      {
        "Index": "NYSE", 
        "LastTradeWithCurrency": "137.24", 
        "LastTradeDateTime": "2016-11-07T13:09:43Z", 
        "LastTradePrice": "137.24", 
        "LastTradeTime": "1:09PM EST", 
        "LastTradeDateTimeLong": "Nov 7, 1:09PM EST", 
        "StockSymbol": "AAP", 
        "ID": "668575"
      }
    ]


    #Trying to solve the issue by loading the json to a string
    resp = json.loads(Apple)

    #print the resp
    print (resp)

    #extract an element in the response
    print (resp["LastTradeWithCurrency"])

1 个答案:

答案 0 :(得分:0)

resp = json.loads(Apple)更改为resp = json.loads(Apple)[0]