Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

json - How can I read this kind of object using python?

I was trying to use requests to read it but I don't know how to

and I tried to look for solutions but I don't know what kind of object is this and don't know how to search for it:

{
  "data": {
    "responsecode": "000",
    "responsemsg": "",
    "datalist": [
      [
        1611192600000,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      [
        1611192600000,
        680.5,
        688.0,
        680.0,
        687.5,
        633200,
        432869750000
      ],
      [
        1611192660000,
        687.5,
        696.0,
        680.5,
        695.5,
        582700,
        402288250000
      ],
      [
        1611192720000,
        695.5,
        699.0,
        695.0,
        698.5,
        839800,
        585819375000
      ],
      [
        1611192780000,
        698.5,
        700.0,
        697.0,
        699.0,
        627100,
        438226800000
      ],
      [
        1611192840000,
        699.0,
        700.0,
        698.0,
        699.5,
        458900,
        320920475000
      ],
      [
        1611192900000,
        700.0,
        700.0,
        699.0,
        699.5,
        231400,
        161913750000
      ],
      [
        1611192960000,
        699.5,
        700.0,
        698.0,
        700.0,
        507800,
        355206850000
      ],
      [
        1611193020000,
        699.5,
        700.0,
        698.5,
        699.0,
        237000,
        165789825000
      ],
      [
        1611193080000,
        699.0,
        700.0,
        698.0,
        699.0,
        310800,
        217270175000
      ],
      [
        1611193140000,
        699.0,
        699.5,
        699.0,
        699.5,
        179900,
        125790125000
      ],
      [
        1611193200000,
        699.0,
        699.5,
        696.152,
        698.0,
        221200,
        154524602000
      ],
      [
        1611193260000,
        698.0,
        699.5,
        698.0,
        699.0,
        226800,
        158496525000
      ],
      [
        1611193320000,
        698.75,
        699.0,
        696.0,
        696.5,
        194000,
        135414500000
      ],
      [
        1611193380000,
        696.5,
        697.5,
        692.5,
        692.5,
        223500,
        155493875000
      ],
      [
        1611193440000,
        692.5,
        694.5,
        690.5,
        690.5,
        299900,
        207736350000
      ],
      [
        1611193500000,
        690.5,
        692.5,
        688.5,
        691.5,
        293600,
        202673350000
      ],
      [
        1611216600000,
        null,
        null,
        null,
        null,
        null,
        null
      ]
    ],
    "start_h": 1,{
  "data": {
    "responsecode": "000",
    "responsemsg": "",
    "datalist": [
      [
        1611192600000,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      [
        1611192600000,
        680.5,
        688.0,
        680.0,
        687.5,
        633200,
        432869750000
      ],
      [
        1611192660000,
        687.5,
        696.0,
        680.5,
        695.5,
        582700,
        402288250000
      ],
      [
        1611192720000,
        695.5,
        699.0,
        695.0,
        698.5,
        839800,
        585819375000
      ],
      [
        1611192780000,
        698.5,
        700.0,
        697.0,
        699.0,
        627100,
        438226800000
      ],
      [
        1611192840000,
        699.0,
        700.0,
        698.0,
        699.5,
        458900,
        320920475000
      ],
      [
        1611192900000,
        700.0,
        700.0,
        699.0,
        699.5,
        231400,
        161913750000
      ],
      [
        1611192960000,
        699.5,
        700.0,
        698.0,
        700.0,
        507800,
        355206850000
      ],
      [
        1611193020000,
        699.5,
        700.0,
        698.5,
        699.0,
        237000,
        165789825000
      ],
      [
        1611193080000,
        699.0,
        700.0,
        698.0,
        699.0,
        310800,
        217270175000
      ],
      [
        1611193140000,
        699.0,
        699.5,
        699.0,
        699.5,
        179900,
        125790125000
      ],
      [
        1611193200000,
        699.0,
        699.5,
        696.152,
        698.0,
        221200,
        154524602000
      ],
      [
        1611193260000,
        698.0,
        699.5,
        698.0,
        699.0,
        226800,
        158496525000
      ],
      [
        1611193320000,
        698.75,
        699.0,
        696.0,
        696.5,
        194000,
        135414500000
      ],
      [
        1611193380000,
        696.5,
        697.5,
        692.5,
        692.5,
        223500,
        155493875000
      ],
      [
        1611193440000,
        692.5,
        694.5,
        690.5,
        690.5,
        299900,
        207736350000
      ],
      [
        1611193500000,
        690.5,
        692.5,
        688.5,
        691.5,
        293600,
        202673350000
      ],
      [
        1611216600000,
        null,
        null,
        null,
        null,
        null,
        null
      ]
    ],
    "start_h": 1,
    "start_m": 30,
    "end_h": 8,
    "end_m": 10
  },
  "qid": "1611236864872"
}
    "start_m": 30,
    "end_h": 8,
    "end_m": 10
  },
  "qid": "1611236864872"
}

My code:

import requests

url = "https://www1.hkex.com.hk/hkexwidget/data/getchartdata2?hchart=1&span=0&int=0&ric=0700.HK&token=evLtsLsBNAUVTPxtGqVeGy7qL7eQdVBTrcCFYFcOPYWI05AgQCleAcsg64QJsAax&qid=1611236864872&callback=jQuery351036507339003734307_1611236863411&_=1611236863417"

get = requests.get(url)
json_data = get.json()

print(json_data)

Error:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    json_data = get.json()
  File "C:UsersuserAppDataLocalProgramsPythonPython37libsite-packages
equestsmodels.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:UsersuserAppDataLocalProgramsPythonPython37libjson\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:UsersuserAppDataLocalProgramsPythonPython37libjsondecoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersuserAppDataLocalProgramsPythonPython37libjsondecoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
question from:https://stackoverflow.com/questions/65829502/how-can-i-read-this-kind-of-object-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How to get the data?

You can parse JSON string by using json.loads() but have to strip() a bit ;)

data = json.loads(r.text.split('(')[1].split(')')[0])['data']

Example

import requests, json
headers = {"user-agent": "Mozilla/5.0"}
url = 'https://www1.hkex.com.hk/hkexwidget/data/getchartdata2?hchart=1&span=0&int=0&ric=0700.HK&token=evLtsLsBNAUVTPxtGqVeGy7qL7eQdVBTrcCFYFcOPYWI05AgQCleAcsg64QJsAax&qid=1611236864872&callback=jQuery351036507339003734307_1611236863411&_=1611236863417'
r = requests.get(url,headers=headers)

data = json.loads(r.text.split('(')[1].split(')')[0])['data']

data

Output

 {'responsecode': '000',
 'responsemsg': '',
 'datalist': [[1611192600000, None, None, None, None, None, None],
  [1611192600000, 680.5, 688.0, 680.0, 687.5, 633200, 432869750000],
  [1611192660000, 687.5, 696.0, 680.5, 695.5, 582700, 402288250000],
  [1611192720000, 695.5, 699.0, 695.0, 698.5, 839800, 585819375000],
  [1611192780000, 698.5, 700.0, 697.0, 699.0, 627100, 438226800000],
  [1611192840000, 699.0, 700.0, 698.0, 699.5, 458900, 320920475000],
  [1611192900000, 700.0, 700.0, 699.0, 699.5, 231400, 161913750000],
  [1611192960000, 699.5, 700.0, 698.0, 700.0, 507800, 355206850000],
  [1611193020000, 699.5, 700.0, 698.5, 699.0, 237000, 165789825000],
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...