2016-11-07 56 views
2

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

预期字符串或缓冲区

我试过在论坛上找到解决方案,但似乎没有任何工作。

我试过的一件事是通过使用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"]) 

回答

0

变化resp = json.loads(Apple)resp = json.loads(Apple)[0]

+0

感谢@OphirCarmi它的工作!不太确定[0]是什么。 –