简体   繁体   English

使用 Python 解析未知数据类型 object Json-like

[英]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.我正在处理Fox 新闻API 并且响应未被识别为我或 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.这是 API 链接以查看整个响应链接我想将响应解析为 json 或任何已知数据类型。

What you see there in the API is called the JSONP format .您在 API 中看到的内容称为JSONP 格式

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:这是一种标准格式,因此您可以从响应中删除括号,然后像普通 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(); 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);如果您删除了评论,即 __jp0(JSON); 是否可以解析它;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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