Skip to main content
 首页 » 编程设计

javascript之Python解析json数据

2025年05月04日58haluo1

我试图解析一个 json 文件并选择一些属性。

这是我的代码

import json 
import urllib 
 
results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or")) 
 
parse_result  = json.loads(results) 
print parse_result  

这里是json数据格式

{ 
  "name": "google_test", 
  "count": 20, 
  "frequency": "Manual Crawl", 
  "version": 1, 
  "newdata": true, 
  "lastrunstatus": "success", 
  "thisversionstatus": "success", 
  "thisversionrun": "Sun Jun 07 2015 17:19:33 GMT+0000 (UTC)", 
  "results": { 
    "collection1": [ 
      { 
        "content": "Parse handles everything you need to store data securely and efficiently in the cloud. Store basic data types, including locations and photos, and query across ...", 
        "title": { 
          "href": "https://parse.com/products/core", 
          "text": "Parse Core | Everything your app needs to save data, be ..." 
        }, 
        "index": 1, 
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data" 
      }, 
      { 
        "content": "Parsing or syntactic analysis is the process of analysing a string of symbols, either ... In order to parse natural language data, researchers must first agree on the  ...", 
        "title": { 
          "href": "http://en.wikipedia.org/wiki/Parsing", 
          "text": "Parsing - Wikipedia, the free encyclopedia" 
        }, 
        "index": 2, 
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data" 
      }, 
      { 
        "content": "In a data flow, Integration Services sources do the work of extracting data, parsing string data, and converting data to an Integration Services data type.", 
        "title": { 
          "href": "https://msdn.microsoft.com/en-us/ms141022.aspx", 
          "text": "Parsing Data - MSDN - Microsoft" 
        } 
 
        ...... 
    ] 
  } 

但是,它向我显示了一些错误。我不明白这是什么。

Traceback (most recent call last): File "get_json_data_2.py", line 6, in parse_result = json.loads(results) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

请您参考如下方法:

results 已包含已解析的 JSON 树。您不需要 loads 部分:

import json 
import urllib 
 
results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or")) 
 
print results