在Python中发出解码JSON URL的问题

时间:2016-04-13 21:41:33

标签: python json time-series urllib2 timeserieschart

我需要从下面提到的网址中提取时间序列数据(时间戳,值)。我正在使用urllib2来获取数据。我能够提取其他url的数据,提供json响应。但是,我无法解码下面显示的URL中的数据。我没有选择更改API的响应。有没有办法以某种方式从网址中提取数据?任何帮助将不胜感激。

import ssl
import urllib2
import json

context = ssl._create_unverified_context()

response = urllib2.urlopen('https://localhost/app?json={"requests":[{"message":"GetHist","id":"XYZ"}]}', context=context)
data = json.load(response)   
print data

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-aaa819691ae6> in <module>()
      6 
      7 response = urllib2.urlopen('https://localhost/app?json={"requests":[{"message":"GetHist","id":"XYZ"}]}', context=context)
----> 8 data = json.loads(response.read())
      9 print data

C:\Users\k2\AppData\Local\Continuum\Anaconda2\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    337             parse_int is None and parse_float is None and
    338             parse_constant is None and object_pairs_hook is None and not kw):
--> 339         return _default_decoder.decode(s)
    340     if cls is None:
    341         cls = JSONDecoder

C:\Users\k2\AppData\Local\Continuum\Anaconda2\lib\json\decoder.pyc in decode(self, s, _w)
    362 
    363         """
--> 364         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    365         end = _w(s, end).end()
    366         if end != len(s):

C:\Users\k2\AppData\Local\Continuum\Anaconda2\lib\json\decoder.pyc in raw_decode(self, s, idx)
    380             obj, end = self.scan_once(s, idx)
    381         except StopIteration:
--> 382             raise ValueError("No JSON object could be decoded")
    383         return obj, end

ValueError: No JSON object could be decoded

来自网址的数据响应:

{ "responses" : [
{
  "message" : "GetHist",
  "id" : "XYZ",
  "rows" : [
    [
      "2016-04-14T00:00:00.000+05:30",
      "80000.0",
      "ok"
    ],
    [
      "2016-04-14T00:15:00.000+05:30",
      "79652.83966064453",
      "ok"
    ],
    [
      "2016-04-14T00:30:00.000+05:30",
      "86999.9008178711",
      "ok"
    ],
    [
      "2016-04-14T00:45:00.000+05:30",
      "80000.0",
      "ok"
    ],
    [
      "2016-04-14T01:00:00.000+05:30",
      "86078.55987548828",
      "ok"
    ]
 ]
}
]}

0 个答案:

没有答案