简体   繁体   中英

Parsing unknown data type object Json-like using Python

I am dealing with Fox news API and the response is not recognized as a known datatype for me or Python. here is a sample of the response:

// API callback
__jp0({
  "kind": "customsearch#search",
  "url": {
  },
  "queries": {
    "request": [
      {
        "totalResults": "791000",
        "count": 10,
        "startIndex": 1,
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "siteSearch": "foxnews.com",
        "siteSearchFilter": "i"
      }
    ],
    "nextPage": [
      {
        "totalResults": "791000",
        "count": 10,
        "startIndex": 11,
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "siteSearchFilter": "i"
      }
    ]})

Here is the API link to see the whole response Link I want to parse the response as json or any known datatype.

What you see there in the API is called the JSONP format .

It's a standard format and so you can just remove parentheses from the response and then load what's inside of parentheses like normal json:

data_json = response_json.split("(", 1)[1].strip(")")
parsed_json = json.loads(data_json)

I don't think you can parse a json with comments by using dict() function or by using json lib, i would recommend using a third party json parse lib, you can find tons of them online and im not sure 100% about the __jp0(); if you can parse it if you removed the comment ie the __jp0(JSON);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM